c++往cuda函数传参的问题,int类型是正确输出,int*则出现问题,同样的问题还有char *
我的cuda函数内容如下
extern "C" __global__ void kernel_run2(void *p1, void *p2){
/*
char *str1 = (char *)p1;
char *str2 = (char *)p2;
printf("p1:%d====p2:%d.\r\n", *str1, *str2);
*/
printf("p1:%d====p2:%d.\r\n", (int *)p1, (int *)p2);
//printf("p1:%s====p2:%s.\r\n", (char *)p1, (char *)p2);
}
这个对于int类型,传入使用&是正确输出的,但是传入了*则不正确
// char *name1 = "zengraoli123";
// char *name2 = "hello";
char name1[] = "zengraoli123";
char name2[] = "hello";
// void *kernelParams[]= {(void *)name1, (void *)name2};
void *kernelParams[]= {(void *)name1, (void *)name2};
// 测试使用int类型
int age1 = 23;
int age2 = 99;
void *kernelParams2[]= {(void *)&age1, (void *)&age2};
cuLaunchKernel(function2, 1, 1, 1, 1, 1, 1, 0, 0, kernelParams, 0);
cudaThreadSynchronize();
printf("Over2\n");
// 测试使用int *类型
int *pAge1 = &age1;
int *pAge2 = &age2;
void *kernelParams3[]= {(void *)pAge1, (void *)pAge2};
cuLaunchKernel(function2, 1, 1, 1, 1, 1, 1, 0, 0, kernelParams3, 0);
cudaThreadSynchronize();
printf("Over3\n");
完整的程序如下
cubin.cu:
#include <stdio.h>
#include <cuda_runtime.h>
extern "C" __global__ void kernel_run(){
printf("hello world!\n");
}
extern "C" __global__ void kernel_run2(void *p1, void *p2){
/*
char *str1 = (char *)p1;
char *str2 = (char *)p2;
printf("p1:%d====p2:%d.\r\n", *str1, *str2);
*/
printf("p1:%d====p2:%d.\r\n", (int *)p1, (int *)p2);
//printf("p1:%s====p2:%s.\r\n", (char *)p1, (char *)p2);
}
main内容如下
#include <stdio.h>
#include <string.h>
#include <cuda_runtime.h>
#include <cuda.h>
int main(){
CUresult error;
CUdevice cuDevice;
cuInit(0);
int deviceCount = 0;
error = cuDeviceGetCount(&deviceCount);
printf("device count is %d\n",deviceCount);
error = cuDeviceGet(&cuDevice, 0);
if(error!=CUDA_SUCCESS){
printf("Error happened in get device!\n");
}
CUcontext cuContext;
error = cuCtxCreate(&cuContext, 0, cuDevice);
if(error!=CUDA_SUCCESS){
printf("Error happened in create context!\n");
}
// 使用编译出来的cubin
CUmodule module;
CUfunction function; // 调用kernel_run
CUfunction function2; // 调用kernel_run2
const char* module_file = "cubin.ptx";
const char* kernel_name = "kernel_run";
const char* kernel_name2 = "kernel_run2";
error = cuModuleLoad(&module, module_file);
if(error!=CUDA_SUCCESS){
printf("Error happened in load moudle %d!\n",error);
}
// 测试用kernel_run函数
error = cuModuleGetFunction(&function, module, kernel_name);
if(error!=CUDA_SUCCESS){
printf("get function error!\n");
}
cuLaunchKernel(function, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0);
cudaThreadSynchronize();
printf("Over\n");
// 测试用kernel_run2函数
error = cuModuleGetFunction(&function2, module, kernel_name2);
if(error!=CUDA_SUCCESS){
printf("get function error!\n");
}
// char *name1 = "zengraoli123";
// char *name2 = "hello";
char name1[] = "zengraoli123";
char name2[] = "hello";
// void *kernelParams[]= {(void *)name1, (void *)name2};
void *kernelParams[]= {(void *)name1, (void *)name2};
// 测试使用int类型
int age1 = 23;
int age2 = 99;
void *kernelParams2[]= {(void *)&age1, (void *)&age2};
cuLaunchKernel(function2, 1, 1, 1, 1, 1, 1, 0, 0, kernelParams, 0);
cudaThreadSynchronize();
printf("Over2\n");
// 测试使用int *类型
int *pAge1 = &age1;
int *pAge2 = &age2;
void *kernelParams3[]= {(void *)pAge1, (void *)pAge2};
cuLaunchKernel(function2, 1, 1, 1, 1, 1, 1, 0, 0, kernelParams3, 0);
cudaThreadSynchronize();
printf("Over3\n");
return 1;
}
--
FROM 159.226.182.*