【发布时间】:2023-03-16 13:59:02
【问题描述】:
我正在使用库 Thrust 来获取两个较大整数集的交集。在使用 2 个小输入的测试中,我得到了正确的结果,但是当我使用具有 10^8 和 65535*1024 元素的两组时,我得到了一个空集。谁能解释一下这个问题?将前两个变量更改为较小的值,推力返回预期的交集。我的代码如下。
#include <thrust/set_operations.h>
#include <thrust/device_vector.h>
#include <thrust/device_ptr.h>
#include <iostream>
#include <stdio.h>
int main() {
int sizeArrayLonger = 100*1000*1000;
int sizeArraySmaller = 65535*1024;
int length_result = sizeArraySmaller;
int* list = (int*) malloc(4*sizeArrayLonger);
int* list_smaller = (int*) malloc(4*sizeArraySmaller);
int* result = (int*) malloc(4*length_result);
int* list_gpu;
int* list_smaller_gpu;
int* result_gpu;
// THE NEXT TWO FORS TRANSFORMS THE SMALLER ARRAY IN A SUBSET OF THE LARGER ARRAY
for (int i=0; i < sizeArraySmaller; i++) {
list_smaller[i] = i+1;
list[i] = i+1;
}
for (int i=sizeArraySmaller; i < sizeArrayLonger; i++) {
list[i] = i+1;
}
cudaMalloc(&list_gpu, sizeof(int) * sizeArrayLonger);
cudaMalloc(&list_smaller_gpu, sizeof(int) * sizeArraySmaller);
cudaMalloc(&result_gpu, sizeof(int) * length_result);
cudaMemcpy(list_gpu, list, sizeof(int) * sizeArrayLonger, cudaMemcpyHostToDevice);
cudaMemcpy(list_smaller_gpu, list_smaller, sizeof(int) * sizeArraySmaller, cudaMemcpyHostToDevice);
cudaMemset(result_gpu, 0, sizeof(int) * length_result);
typedef thrust::device_ptr<int> device_ptr;
thrust::set_intersection(device_ptr(list_gpu), device_ptr(list_gpu + sizeArrayLonger), device_ptr(list_smaller_gpu),
device_ptr(list_smaller_gpu + sizeArraySmaller), device_ptr(result_gpu), thrust::less<int>() );
// MOVING TO CPU THE MARKER ARRAY OF ELEMENTS OF INTERSECTION SET
cudaMemcpy(result, result_gpu, sizeof(int)*length_result, cudaMemcpyDeviceToHost);
cudaDeviceSynchronize();
// THIS LOOP ITERATES ALL ARRAY NAMED "result" WHERE THE POSITION ARE MARKED WITH 1
int counter = 0;
for (int i=0; i < length_result; i++)
if (result[i]) {
printf("\n-> %d", result[i]);
counter++;
}
printf("\nTHRUST -> Total of elements: %d\n", counter);
cudaDeviceReset();
return 0;
}
【问题讨论】:
-
你真的确定你的 GPU 有足够的可用内存来处理如此大的数组大小吗?我计算了大约 1Gb 的 cudaMalloc 内存,没有代码开销和推力可能需要的中间存储。
-
您的代码在配备 Quadro5000 GPU(约 2.5GB 内存)和 CUDA 7 的 Fedora 20 系统上似乎可以正常工作。我得到了 67107842 行输出,最后打印了
THRUST -> Total of elements: 67107840。但是,当我在具有 1GB 内存的 GeForce GT 640 GPU 上运行它时,我得到THRUST -> Total of elements: 0除此之外,在这种情况下,推力似乎默默地失败了,这有点不寻常。您在哪种系统上运行? -
我在 Windows 8.1、Cuda Toolkit 6.5 中运行,我的 GPU 是 GeForce 740M 和 2GB。另一次我用相同长度的数组执行了我自己的交集代码,我得到了预期的结果。我不知道 Thrust 是如何实现你自己的交集算法的。
-
我自己的具有相同长度数组的交集算法在我的 GPU 中运行。但我不知道此类算法之前是否运行过排序。我想这是推力算法项目固有的东西。
-
Thrust 可以在后台进行各种临时内存分配。
标签: cuda nvidia intersection thrust