EDIT: Il y avait un example here mais il n'est plus trouvé, mais la plupart de l'exemple a été copié ci-dessous.
L'appelant C (mais pourrait être C++)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cuda.h>
extern void kernel_wrapper(int *a, int *b);
int main(int argc, char *argv[])
{
int a = 2;
int b = 3;
kernel_wrapper(&a, &b);
return 0;
}
Le Callee (CUDA)
__global__ void kernel(int *a, int *b)
{
int tx = threadIdx.x;
switch(tx)
{
case 0:
*a = *a + 10;
break;
case 1:
*b = *b + 3;
break;
default:
break;
}
}
void kernel_wrapper(int *a, int *b)
{
int *d_1, *d_2;
dim3 threads(2, 1);
dim3 blocks(1, 1);
cudaMalloc((void **)&d_1, sizeof(int));
cudaMalloc((void **)&d_2, sizeof(int));
cudaMemcpy(d_1, a, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_2, b, sizeof(int), cudaMemcpyHostToDevice);
kernel<<< blocks, threads >>>(a, b);
cudaMemcpy(a, d_1, sizeof(int), cudaMemcpyDeviceToHost);
cudaMemcpy(b, d_2, sizeof(int), cudaMemcpyDeviceToHost);
cudaFree(d_1);
cudaFree(d_2);
}
Pourquoi ne pas simplement essayer? :) Et oui, cette fonction particulière cudaMemcpy() peut être appelée à partir d'un fichier C. En passant, je trouve la documentation cuda terrible dans ce genre de détails. – Slava
Suite à la réponse de Preet: vous devrez également faire un lien avec 'cudart' pour satisfaire l'éditeur de liens. C'est à dire. nvcc -lcudart myfile.cpp – Edric