向CUDA中传入多维结构体
逻辑相对复杂,一些应用中也不一定用的上传入多维结构体,所以不做文字解释。一个样例程序放在这里为大家提供思路,供大家参考。如果有什么疑问欢迎大家评论,我会及时回复。
/**
* Copyright 1993-2012 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*/
#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
struct cu_minimatrix
{
size_t size1;
size_t size2;
size_t tda;//physical row dimension
double * data;
int owner;
};
struct cu_Factor{
int size;
int *factorsize;
int *factortype;
cu_minimatrix *sqrt_information_;
int **keys_;
};
__global__ void test (cu_Factor *p,double* d){
int id = blockDim.x * blockIdx.x + threadIdx.x;
p->sqrt_information_->data[id]+=1;
d[id]=p->sqrt_information_->data[id];
}
int main(int argc, char **argv)
{
cu_minimatrix *h_matrix = new cu_minimatrix;
cu_minimatrix *d_matrix;
cu_Factor *h_factor = new cu_Factor;
cu_Factor *d_factor;
double* d;
double* data = (double*)malloc(sizeof(double)*3);
double* h = (double*)malloc(sizeof(double)*3);
double arr[3];
cudaMalloc(&d,sizeof(double)*3);
data[0]=7;
data[1]=4;
data[2]=0;
h_matrix->size1 = 1;
h_matrix->size2 = 3;
h_matrix->tda = 1;
h_matrix->owner = 1;
cudaMalloc(&h_matrix->data,sizeof(double)*3);
cudaMemcpy(h_matrix->data, data, sizeof(double)*3, cudaMemcpyHostToDevice);
cudaMalloc(&d_matrix,sizeof(cu_minimatrix ));
cudaMemcpy(d_matrix,h_matrix,sizeof(cu_minimatrix),cudaMemcpyHostToDevice);
h_factor->sqrt_information_=d_matrix;
cudaMalloc(&d_factor,sizeof(cu_Factor));
cudaMemcpy(d_factor,h_factor,sizeof(cu_Factor),cudaMemcpyHostToDevice);
test<<<1,3,0 >>>(d_factor,d);
cudaMemcpy(h,d,sizeof(double)*3,cudaMemcpyDeviceToHost);
for(int i=0;i<3;i++){
arr[i]=h[i];
printf("%.2lf\n",arr[i]);
}
printf("good");
return 0;
}