更大此代码工作正常:无效配置参数块的16位比
#include <stdio.h>
#define N 1000 // <-- Works for values < 2^16
__global__
void add(int *a, int *b) {
int i = blockIdx.x;
if (i<N) {
b[i] = 2*a[i];
}
}
int main() {
int max_value[2];
int ha[N], hb[N];
int *da, *db;
cudaMalloc((void **)&da, N*sizeof(int));
cudaMalloc((void **)&db, N*sizeof(int));
for (int i = 0; i<N; ++i) {
ha[i] = i;
}
cudaMemcpy(da, ha, N*sizeof(int), cudaMemcpyHostToDevice);
add<<<N, 1>>>(da, db);
cudaMemcpy(hb, db, N*sizeof(int), cudaMemcpyDeviceToHost);
max_value[0] = hb[0];
int i;
for (i = 0; i < N; i++) {
if (hb[i] > max_value[0]) {
max_value[0] = hb[i];
max_value[1] = i;
}
}
cudaFree(da);
cudaFree(db);
printf("Max number %d, from value:%d \n", max_value[0], max_value[1]);
getchar();
return 0;
}
但是当我从1000改变数字N
(数组中的项)>(2 )-1-程序崩溃。
我认为这是对东道国的溢出,让我感动的ha
和hb
数组声明BSS segment
和改变N
到100万。
#include <stdio.h>
#define N 1000000 // <----
__global__
void add(int *a, int *b) {
int i = blockIdx.x;
if (i<N) {
b[i] = 2*a[i];
}
}
static int ha[N]; // <----
static int hb[N]; // <----
int main() {
int max_value[2];
// int ha[N], hb[N];
int *da, *db;
cudaMalloc((void **)&da, N*sizeof(int));
cudaMalloc((void **)&db, N*sizeof(int));
for (int i = 0; i<N; ++i) {
ha[i] = i;
}
cudaMemcpy(da, ha, N*sizeof(int), cudaMemcpyHostToDevice);
add<<<N, 1>>>(da, db);
cudaMemcpy(hb, db, N*sizeof(int), cudaMemcpyDeviceToHost);
max_value[0] = hb[0];
int i;
for (i = 0; i < N; i++) {
if (hb[i] > max_value[0]) {
max_value[0] = hb[i];
max_value[1] = i;
}
}
cudaFree(da);
cudaFree(db);
printf("Max number %d, from value:%d \n", max_value[0], max_value[1]);
getchar();
return 0;
}
现在我没有得到一个错误,但hb
数组为空。
我的代码有什么问题?
如何分配大数组到设备并获得有效结果?
更新:我已经插入错误检查代码,
我得到的错误是 - >“无效的配置参数”。
更新代码是:
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <thrust/system_error.h>
#include <thrust/system/cuda/error.h>
#include <sstream>
const int N = 70000;
#define checkCudaErrors(error) {\
if (error != cudaSuccess) {\
printf("CUDA Error - %s:%d: '%s'\n",__FILE__,__LINE__,cudaGetErrorString(error));\
exit(1);\
}\
}\
__global__
void add(int *a, int *b) {
int i = blockIdx.x;
if (i<N) {
b[i] = 2*a[i];
}
}
static int ha[N];
static int hb[N];
int main() {
// int ha[N], hb[N];
int max_value[2];
int deviceCount = 0;
cudaGetDeviceCount(&deviceCount);
cudaError_t err=cudaDeviceReset();
if(err!=cudaSuccess){printf("%s in %s at line %d\n",cudaGetErrorString(err),__FILE__,__LINE__);}
printf("Device count: %d \n", deviceCount);
for (int i = 0; i<N; ++i) { ha[i] = i; }
int *da, *db;
checkCudaErrors(cudaMalloc((void **)&da, N*sizeof(int)));
checkCudaErrors(cudaMalloc((void **)&db, N*sizeof(int)));
checkCudaErrors(cudaMemcpy(da, ha, N*sizeof(int), cudaMemcpyHostToDevice));
add<<<N, 1>>>(da, db); // <--- Invalid configuration error
checkCudaErrors(cudaMemcpy(hb, db, N*sizeof(int), cudaMemcpyDeviceToHost));
max_value[0] = hb[0];
int i;
for (i = 0; i < N; i++) {
if (hb[i] > max_value[0]) {
max_value[0] = hb[i];
max_value[1] = i;
}
}
cudaError_t error = cudaGetLastError();
if(error != cudaSuccess) {
printf("CUDA error: %s\n", cudaGetErrorString(error));
getchar();
exit(-1);
}
getchar();
return 0;
}
该设备是一个的GeForce GTX 470和我使用
NVCC -o FOO new.cu编译
'cudaMalloc'和'cudaMemcpy'都返回'cudaError_t'类型的值 - 可能值得先检查一下。 – iehrlich
谢谢@iehrlich我会检查这个 –
也请看看[this](https://stackoverflow.com/questions/34655893/cuda-large-input-arrays)和总体[this](https:// www.google.ru/search?q=cuda+large+array)可能会给你一些提示。祝你好运! – iehrlich