关于thrust的使用的一些总结

void gpuInit()
{

	cudaSetDevice(0);

	thrust::host_vector m(5); //主机向量
	thrust::sequence(m.begin(),m.end());//顺序插入数0 1 2 3 ....
	thrust::device_vector d_v(5);//设备向量
	thrust::device_vector d_vv(5);//二维设备向量

	double A[5][4];			
	for(int i = 0;i < 5; ++i)
	{
		for(int j = 0; j < 4; ++j)
		{
			A[i][j] = i + j;
		}
	}

	for(int i = 0; i < 5; ++i)
	{
		d_vv[i] = A[i];		//给二维向量设备向量赋值

	}
	std::cout << "device_vector is size = " << d_vv.size() << std::endl;

	for(int i = 0; i < 5; ++i)
	{
		for(int j = 0; j < 4; ++j)
		{
			std::cout << "d_vv[" << i << "][" << j << "] = " << d_vv[i][j]<< " ";
		}
		std::cout << std::endl;
	}


	d_v = m;  //内存数据复制到显存里,一维数据比较好用
	for(int i = 0; i < m.size() ; ++i)
	{
		std::cout << "m[i] = " << m[i] << "  ";
	}
	std::cout << std::endl;

	for(int i = 0; i < 5 ; ++i)
	{
		std::cout << "d_v = " << d_v[i] << "  ";
	}
	std::cout << std::endl;

	thrust::host_vector > h(5,thrust::host_vector(5));//主机的二维向量可以这样申请,设备的就不行尝试了好多都不行


	std::cout <<"host_vector to device:" << std::endl;
	std::cout << "device_vector is size = " << d_vv.size() << std::endl;

	for(int i = 0; i < 5; ++i)
	{
		for(int j = 0; j < 5; ++j)
		{
			std::cout << "d_vv[" << i << "][" << j << "] = " << d_vv[i][j]<< " ";
		}
		std::cout << std::endl;
	}


	std::cout << "host_vector is size = " << h.size() << std::endl;
	for(int i = 0; i < 5; ++i)
	{
		for(int j = 0; j < 5;++j)
		{
			std::cout << h[i][j] << "  ";
		}
		std::cout << std::endl;
	}
}

 

你可能感兴趣的:(CUDA,C)