我可以从C ++代码编译不是由nvcc编译CUDA运行时函数? [英] Can I call CUDA runtime function from C++ code not compiled by nvcc?
本文介绍了我可以从C ++代码编译不是由nvcc编译CUDA运行时函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以调用CUDA运行时函数调用如
cudaMemcpy(...)使用常规C ++编译器编译的.cpp文件中的
?
解决方案
编辑:此处有一个示例
调用者C(但可能是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;
}
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;
默认值:
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<<块,线程>>(a,b);
cudaMemcpy(a,d_1,sizeof(int),cudaMemcpyDeviceToHost);
cudaMemcpy(b,d_2,sizeof(int),cudaMemcpyDeviceToHost);
cudaFree(d_1);
cudaFree(d_2);
}
Is there any way I can call CUDA runtime function calls such as
cudaMemcpy(...);
in a .cpp file, compiled with a regular C++ compiler?
解决方案
EDIT: There was an example here but it's not longer found, but most of the example was copied below.
The caller C (but could be 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;
}
The 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);
}
这篇关于我可以从C ++代码编译不是由nvcc编译CUDA运行时函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文