cuda中結構體的賦值

#include <stdio.h>
#include <cutil.h>

#define N 10

typedef struct
{
	int *A;
	int *B;
}vn;

typedef struct
{
	vn *v_n;
	int *m;
	int *n;
}data;

__global__ void kernel(data *d, int n)
{
	int idx = threadIdx.x;
	if(idx < n)
	{
		d->v_n->A[idx] = d->v_n->A[idx] + *d->m;
		d->v_n->B[idx] = d->v_n->B[idx] + *d->n;
	}
}

int main(int argc, char **argv)
{
	CUT_DEVICE_INIT(argc, argv);


	int i;	
	data h_input;
	h_input.m = (int *)malloc(sizeof(int));
	h_input.n = (int *)malloc(sizeof(int));
	h_input.v_n = (vn *)malloc(sizeof(vn));
	h_input.v_n->A = (int *)malloc(sizeof(int) * N);
	h_input.v_n->B = (int *)malloc(sizeof(int) * N);

	*h_input.m = 5;
	*h_input.n = 10;
	for(i = 0; i < N; ++i)
	{
		h_input.v_n->A[i] = i;
		h_input.v_n->B[i] = i;
	}

    //在顯存上定義結構體tmp,使用過渡變量,如果包含多級結構體就需要使用多個過渡變量
	data tmp;
	cudaMalloc((void**)& tmp.m, sizeof(int));
	cudaMalloc((void**)& tmp.n, sizeof(int));
	cudaMalloc((void**)& tmp.v_n, sizeof(vn));
	
	vn VN;
	cudaMalloc((void**)&VN.A, sizeof(int) * N);
	cudaMalloc((void**)&VN.B, sizeof(int) * N);
	cudaMemcpy(tmp.v_n, &VN, sizeof(vn), cudaMemcpyHostToDevice);
	

	data *d_input;
	cudaMalloc((void**)&d_input, sizeof(data));
	cudaMemcpy(d_input, &tmp, sizeof(data), cudaMemcpyHostToDevice);
	
    //將數據拷貝到顯存中,要使用先前定義的過渡變量
	cudaMemcpy(tmp.m, h_input.m, sizeof(int), cudaMemcpyHostToDevice);
	cudaMemcpy(tmp.n, h_input.n, sizeof(int), cudaMemcpyHostToDevice);
	cudaMemcpy(VN.A, h_input.v_n->A, sizeof(int) * N, cudaMemcpyHostToDevice);	
	cudaMemcpy(VN.B, h_input.v_n->B, sizeof(int) * N, cudaMemcpyHostToDevice);	
	
    kernel<<<1, N>>>(d_input, N);

    //將數據從顯存拷貝到內存中,也需要使用過渡變量來完成,先拷貝結構體變量,然後在拷貝基本類型的數據
	data tmp1;
	cudaMemcpy(&tmp1, d_input, sizeof(data), cudaMemcpyDeviceToHost);
	vn VN1;
	cudaMemcpy(&VN1, tmp1.v_n, sizeof(vn), cudaMemcpyDeviceToHost);

	cudaMemcpy(h_input.v_n->A, VN1.A, sizeof(int) * N, cudaMemcpyDeviceToHost);
	cudaMemcpy(h_input.v_n->B, VN1.B, sizeof(int) * N, cudaMemcpyDeviceToHost);

	
	for(i = 0; i < N; ++i)
		printf("%d ", h_input.v_n->A[i]);
	printf("\n");
	for(i = 0; i < N; ++i)
		printf("%d ", h_input.v_n->B[i]);
	printf("\n");
	
	CUT_EXIT(argc, argv);
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章