![](/img/trans.png)
[英]ptxas “double is not supported” warning when using thrust::sort on a struct array
[英]How to use a struct array in thrust::sort?
我對 CUDA 開發還很陌生,我正在嘗試使用推力庫的排序方法對結構數組進行排序。 我的結構是這樣的:
#define N 307200
struct distanceVector {
Point3D a, b;
float distance;
};
我想按“距離”對數組進行排序,但是,排序函數需要兩個隨機訪問迭代器,而且由於我沒有使用向量,因此我沒有任何向量。 我試過做這樣的事情:
bool distance_sort(distanceVector A, distanceVector B){
return (A.distance > B.distance);
}
distanceVector * MyStructArray;
cudaMalloc((void**)&MyStructArray, sizeof(distanceVector) * N);
//LAUNCH KERNEL WHICH FILLS MYSTRUCTARRAY AND THEN...
thrust::sort(MyStructArray, MyStructArray + N, distance_sort);
...我在[推力指南][1]中看到的一個例子:
#include <thrust/sort.h>
#include <thrust/functional.h>
...
const int N = 6;
int A[N] = {1, 4, 2, 8, 5, 7};
thrust::stable_sort(A, A + N, thrust::greater<int>());
// A is now {8, 7, 5, 4, 2, 1}
盡管它可以編譯,但在執行過程中我得到一個“訪問沖突讀取位置 0x405e041c”。 錯誤。 調試應用程序時,在 insert_sort.h 文件中的這一部分停止:
for(RandomAccessIterator i = first + 1; i != last; ++i)
{
value_type tmp = *i;
if (wrapped_comp(tmp, *first)).......
有沒有辦法在不使用推力矢量的情況下解決這個問題?
好的,所以我發現了我的錯誤。 問題是我試圖在設備上分配的內存上使用推力。 我嘗試先將 MyStructArray 復制到主機設備,然后使用推力排序,並且效果很好。 為了在設備的內存上工作,我必須使用推力::device_ptr<MyStruct> 指針(MyStructArray)。 希望這對其他人有幫助。
我嘗試使用thrust::sort
對thrust::host_vector<struct>
和thrust::device_vector<struct>
進行排序。 它們都在我的設備中運行良好。
這是我的代碼:
#include <bits/stdc++.h>
#include <stdio.h>
#include <cuda_runtime.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <time.h>
#include <thrust/functional.h>
const int MAXN = 1e2;
typedef struct Node {
int x, y;
__host__ __device__ bool operator < (const Node& a) const {
if (x == a.x) {
return x ? y<a.y : y>a.y;
}else
return x < a.x;
}
}Node;
int main() {
thrust::host_vector<Node> h(MAXN),ne(MAXN);
for (int i = 0; i < MAXN; i++) {
h[i].x = rand() % 2;
h[i].y = rand() % 997;
}
thrust::device_vector<Node> d = h;
thrust::sort(h.begin(), h.end());
thrust::sort(d.begin(), d.end());
ne = d;
for (int i = 0; i < MAXN; i++) {
std::cout << h[i].x << " " << h[i].y << " | " << ne[i].x << " " << ne[i].y << std::endl;
}
}
我想在設備上對我的自定義結構數組進行排序。 但是當我編譯代碼時,它發生錯誤。 這是我的代碼:
struct com {
__device__ bool operator()(Particle a, Particle b) {
return a.cell_idx > b.cell_idx;
}
particle_ptr 是我的設備指針。
thrust::device_ptr<Particle> pointer(particle_ptr);
thrust::sort(thrust::device_pointer_cast(particle_ptr), thrust::device_pointer_cast(particle_ptr + C), com()); // 1 method
thrust::sort(pointer, pointer + C, com()); // 2 method
無論使用哪種方法都會報錯如下: /usr/local/cuda-10.0/include/thrust/system/cuda/detail/sort.h(884): error: identifier "xxx::Particle: :Particle" 在設備代碼中未定義
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.