zoukankan      html  css  js  c++  java
  • 命令行cpp与cu文件混合编译

    首先这里有两段代码:

    main.cpp:

    复制代码
    #include <stdio.h>
    #include <iostream>
    
    extern "C"
    {
    int func();  
    }
    
    int main()
    {
        std::cout<<"Hello C++"<<std::endl;
        func();
        return 0;
    }
    复制代码

    test.cu:

    复制代码
    #include <cuda_runtime.h>
    #include <stdio.h>
    
    //thread 1D
    __global__ void testThread1(int *c, const int *a, const int *b)
    {
        int i = threadIdx.x;
        c[i] = b[i] - a[i];
    }
    
    void addWithCuda(int *c, const int *a, const int *b, unsigned int size)
    {
        int *dev_a = 0;
        int *dev_b = 0;
        int *dev_c = 0;
    
        cudaSetDevice(0);
    
        cudaMalloc((void**)&dev_c, size * sizeof(int));
        cudaMalloc((void**)&dev_a, size * sizeof(int));
        cudaMalloc((void**)&dev_b, size * sizeof(int));
    
        cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);
        cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);
    
        testThread1<<<1, size>>>(dev_c, dev_a, dev_b);
    
        cudaMemcpy(c, dev_c, size*sizeof(int), cudaMemcpyDeviceToHost);
    
        cudaFree(dev_a);
        cudaFree(dev_b);
        cudaFree(dev_c);
    
        cudaGetLastError();
    }
    
    extern "C" 
    int func() 
    {
        const int n = 1000;
    
        int *a = new int[n];
        int *b = new int[n];
        int *c = new int[n];
        int *cc = new int[n];
    
        for (int i = 0; i < n; i++)
        {
            a[i] = rand() % 100;
            b[i] = rand() % 100;
            c[i] = b[i] - a[i];
        }
    
        addWithCuda(cc, a, b, n);
    
        FILE *fp = fopen("out.txt", "w");
        for (int i = 0; i < n; i++)
            fprintf(fp, "%d %d
    ", c[i], cc[i]);
        fclose(fp);
    
        bool flag = true;
        for (int i = 0; i < n; i++)
        {
            if (c[i] != cc[i])
            {
                flag = false;
                break;
            }
        }
    
        if (flag == false)
            printf("no pass");
        else
            printf("pass");
    
        cudaDeviceReset();
    
        delete[] a;
        delete[] b;
        delete[] c;
        delete[] cc;
    
        return 0;
    }
    复制代码

    Linux下可以这样:

    nvcc -c test.cu
    g++ -c main.cpp
    g++ -o main.o test.o -lcudart -L/usr/local/cuda/lib64

    Windows下可以这样:

    nvcc -c test.cu
    cl -c main.cpp
    link main.obj test.obj cudart.lib -libpath:"C:Program FilesNVIDIA GPU Computing ToolkitCUDAv10.2libx64"

    应该都差不多。

  • 相关阅读:
    Service Cloud 零基础(五)Trailhead学习 Embedded Chat
    Community Cloud零基础学习(五)Topic(主题)管理
    Service Cloud 零基础(四)快速配置一个问卷调查(无开发)
    salesforce零基础学习(一百)Mobile Device Tracking
    mysql 设置查询超时配置
    YIi2 Object 报错问题
    php 如何创建uuid
    mysql8 安装后无法登录的问题
    nano编辑器保存退出
    在使用openbms的时候发现的Thinkphp action 大小写问题
  • 原文地址:https://www.cnblogs.com/ybsport/p/12321415.html
Copyright © 2011-2022 走看看