vendredi 23 novembre 2018

CUDA - separating cpu code from cuda code

Was looking to use system functions (such as rand() ) within the CUDA kernel. However, ideally this would just run on the CPU. Can I separate files (.cu and .c++), while still making use of gpu matrix addition? For example, something along these lines:

in main.cpp:

int main(){
std::vector<int> myVec;
srand(time(NULL));

for (int i = 0; i < 1024; i++){
    myvec.push_back( rand()%26);
}

selfSquare(myVec, 1024);

}

and in cudaFuncs.cu:

__global__ void selfSquare_cu(int *arr, n){
    int i = threadIdx.x;
    if (i < n){
        arr[i] = arr[i] * arr[i];
    }

}

void selfSquare(std::vector<int> arr, int n){
    int *cuArr;
    cudaMallocManaged(&cuArr, n * sizeof(int));
    for (int i = 0; i < n; i++){
        cuArr[i] = arr[i];
    }

    selfSquare_cu<<1, n>>(cuArr, n);
}

What are best practices surrounding situations like these? Would it be a better idea to use curand and write everything in the kernel? It looks to me like in the above example, there is an extra step in taking the vector and copying it to the shared cuda memory.

Aucun commentaire:

Enregistrer un commentaire