我有一个cuda推力计划
#include <stdio.h>
#include<iostream>
#include <cuda.h>
#include <thrust/sort.h>
// main routine that executes on the host
int main(void)
{
int *a_h, *a_d; // Pointer to host & device arrays
const int N = 10; // Number of elements in arrays
size_t size = N * sizeof(int);
a_h = (int *)malloc(size); // Allocate array on host
cudaMalloc((void **) &a_d, size);// Allocate array on device
std::cout<<"enter the 10 numbers";
// Initialize host array and copy it to CUDA device
for (int i=0; i<N; i++)
{
std::cin>>a_h[i];
}
for (int i=0; i<N; i++) printf("%d %d\n", i, a_h[i]);
cudaMemcpy(a_d, a_h, size, cudaMemcpyHostToDevice);
thrust::sort(a_d, a_d + N);
// Do calculation on device:
cudaMemcpy(a_h, a_d, sizeof(int)*N, cudaMemcpyDeviceToHost);
// Print results
for (int i=0; i<N; i++) printf("%d %d\n", i, a_h[i]);
// Cleanup
free(a_h); cudaFree(a_d);
}
Run Code Online (Sandbox Code Playgroud)
但它没有运行以提供所需的输出.
我们是否应该使用主机矢量和设备矢量进行推力排序????
对于设备操作,您应该使用设备指针或device_vector迭代器,而不是原始指针.原始指针(指向主机内存)可用于主机上的操作.
因此,如果您修改代码如下:
#include <thrust/device_ptr.h>
...
thrust::device_ptr<int> t_a(a_d); // add this line before the sort line
thrust::sort(t_a, t_a + N); // modify your sort line
Run Code Online (Sandbox Code Playgroud)
我相信它会对你有用.
您可能希望阅读推力快速入门指南.特别注意本节:
您可能想知道当"原始"指针用作Thrust函数的参数时会发生什么.与STL一样,Thrust允许这种用法,它将调度算法的主机路径.如果有问题的指针实际上是指向设备内存的指针,那么在调用函数之前你需要用thrust :: device_ptr包装它
| 归档时间: |
|
| 查看次数: |
1994 次 |
| 最近记录: |