पर इंटर-ब्लॉक बाधा मैं सीयूडीए पर एक इंटर-ब्लॉक बाधा लागू करना चाहता हूं, लेकिन गंभीर समस्या का सामना करना चाहता हूं।सीयूडीए
मुझे नहीं पता कि यह क्यों काम नहीं करता है।
#include <iostream>
#include <cstdlib>
#include <ctime>
#define SIZE 10000000
#define BLOCKS 100
using namespace std;
struct Barrier {
int *count;
__device__ void wait() {
atomicSub(count, 1);
while(*count)
;
}
Barrier() {
int blocks = BLOCKS;
cudaMalloc((void**) &count, sizeof(int));
cudaMemcpy(count, &blocks, sizeof(int), cudaMemcpyHostToDevice);
}
~Barrier() {
cudaFree(count);
}
};
__global__ void sum(int* vec, int* cache, int *sum, Barrier barrier)
{
int tid = blockIdx.x;
int temp = 0;
while(tid < SIZE) {
temp += vec[tid];
tid += gridDim.x;
}
cache[blockIdx.x] = temp;
barrier.wait();
if(blockIdx.x == 0) {
for(int i = 0 ; i < BLOCKS; ++i)
*sum += cache[i];
}
}
int main()
{
int* vec_host = (int *) malloc(SIZE * sizeof(int));
for(int i = 0; i < SIZE; ++i)
vec_host[i] = 1;
int *vec_dev;
int *sum_dev;
int *cache;
int sum_gpu = 0;
cudaMalloc((void**) &vec_dev, SIZE * sizeof(int));
cudaMemcpy(vec_dev, vec_host, SIZE * sizeof(int), cudaMemcpyHostToDevice);
cudaMalloc((void**) &sum_dev, sizeof(int));
cudaMemcpy(sum_dev, &sum_gpu, sizeof(int), cudaMemcpyHostToDevice);
cudaMalloc((void**) &cache, BLOCKS * sizeof(int));
cudaMemset(cache, 0, BLOCKS * sizeof(int));
Barrier barrier;
sum<<<BLOCKS, 1>>>(vec_dev, cache, sum_dev, barrier);
cudaMemcpy(&sum_gpu, sum_dev, sizeof(int), cudaMemcpyDeviceToHost);
cudaFree(vec_dev);
cudaFree(sum_dev);
cudaFree(cache);
free(vec_host);
return 0;
}
वास्तव में, यहां तक कि अगर मैं इंतजार पुनर्लेखन() निम्नलिखित
__device__ void wait() {
while(*count != 234124)
;
}
कार्यक्रम के रूप में सामान्य रूप से बाहर निकालता है। लेकिन मुझे इस मामले में एक अनंत लूप मिलने की उम्मीद है।
मुझे संदेह है कि आपका कर्नेल वास्तव में '' बैरियर :: प्रतीक्षा''' के अंदर एक खराब सूचक को अस्वीकार करने के कारण क्रैश हो रहा है। कर्नेल के दौरान त्रुटि की जांच के लिए '' 'cudaGetLastError''' का प्रयोग करें। –