向CUDA中传入多维结构体

向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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值