2016-12-20 2 views
0

Dies ist meine Kernfunktion und es tut einfache Arbeit: & jedes Element mit blockIdx.x sogar filtern und ungerade:CUDA Programm Rückkehr zufällige Ergebnis

__global__ 
void g_compact(const unsigned int* v_array, unsigned int* compact_array, int size) 
{ 
    const int p_index = blockIdx.x * size + threadIdx.x; 

    if ((v_array[threadIdx.x] & 1) == blockIdx.x) 
    { 
     compact_array[p_index]= 1; 
    } 
    else 
    { 
     compact_array[p_index]= 0; 
    } 
} 

es jedoch zufälliges Ergebnis produziert jedes Mal, wenn ich das Programm ausführen wie

1 0 1625730008 32767 1625730024 32767 4197775 0 0 0 4197470 0 0 0 2525809656 32630 1 0 1625729712 32767

Was verwirrt ich bin, dass das Ergebnis nicht 0 oder 1 da meine if und else sollte jede Situation abdecken.

Könnte mir jemand dabei helfen?

Gesamtprogramm:

#include <iostream> 

void print_array(const unsigned int* v_array, int size) 
{ 
    for (int i = 0; i < size; ++i) 
    { 
     std::cout<<v_array[i]<<" "; 
    } 
    std::cout<<std::endl; 
} 

__global__ 
void g_compact(const unsigned int* v_array, unsigned int* compact_array, int size) 
{ 
    const int p_index = blockIdx.x * size + threadIdx.x; 

    if (true) 
    { 
     compact_array[p_index]= 1; 
    } 
    else 
    { 
     compact_array[p_index]= 0; 
    } 
} 

int main(int argc, char const *argv[]) 
{ 
    unsigned int *d_in; 
    unsigned int *d_out; 

    cudaMalloc(&d_in, sizeof(unsigned int) * 10); 
    cudaMalloc(&d_out, sizeof(unsigned int) * 20); 

    unsigned int h_array[10] = { 
     1, 2, 3, 4, 
     5, 6, 7, 8, 
     9, 10 
    }; 

    cudaMemcpy(d_in, h_array, sizeof(unsigned int) * 10, cudaMemcpyHostToDevice); 

    g_compact<<<2, 10>>>(h_array, d_out, 10); 

    unsigned int h_out[20]; 
    cudaMemcpy(h_out, d_out, sizeof(unsigned int) * 20, cudaMemcpyDeviceToHost); 

    print_array(h_out, 20); 

    return 0; 
} 
+0

Eine gute CUDA Fehlerprüfung geholfen hätte ändern wollen Sie Ihnen Hinweise geben auf das, was geschah, vielleicht haben Sie einen Blick auf haben könnte: http: // Stackoverflow. com/questions/14038589/what-is-the-canonical-way-to-check-für-fehler-using-the-cuda-runtime-api für Ihre zukünftigen CUDA-Entwicklungen – X3liF

Antwort

1

Das Problem ist nicht, wie Sie Ihre Kernel-Funktion schreiben, sondern wie Sie es nennen:

unsigned int h_array[10] = { 
    1, 2, 3, 4, 
    5, 6, 7, 8, 
    9, 10 
}; 

cudaMemcpy(d_in, h_array, sizeof(unsigned int) * 10, cudaMemcpyHostToDevice); 

g_compact<<<2, 10>>>(h_array, d_out, 10); 

Sie übergeben einen Hostzeiger (h_array) zu einer Kernfunktion. Wie soll es funktionieren?

Ich glaube, Sie h_array zu d_in

g_compact<<<2, 10>>>(d_in, d_out, 10); 
Verwandte Themen