Ce code fonctionne très bien:argument configuration non valide pour fil bloquer supérieure à 16bit
#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;
}
Mais quand changer le numéro N
(éléments du tableau) de 1000 à> (2) -1 le programme se bloque.
Je pensais qu'il était un débordement sur l'hôte, donc je me suis déplacé la déclaration de tableau de ha
et hb
-BSS segment
et changé N
-1000000.
#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;
}
Maintenant, je ne suis pas une erreur, mais le tableau hb
est vide.
Quel est le problème avec mon code?
Comment puis-je allouer de grandes baies à un périphérique et obtenir un résultat valide?
JOUR: J'ai inséré le code de vérification d'erreur, l'erreur
je reçois est ->« arguments Configuration invalide ».
Le code mis à jour est le suivant:
#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;
}
Le dispositif est une GeForce GTX 470 et je suis en utilisant la compilation
nvcc de new.cu de foo
'cudaMalloc' et' cudaMemcpy' renvoient tous les deux une valeur de type 'cudaError_t' - cela peut valoir la peine d'être vérifié. – iehrlich
Merci @iehrlich Je vais vérifier cela –
Jetez également un coup d'oeil à [this] (https://stackoverflow.com/questions/34655893/cuda-large-input-arrays), et dans l'ensemble [this] (https: // www.google.ru/search?q=cuda+large+array) pourrait vous donner quelques indices. Bonne chance! – iehrlich