samedi 4 juillet 2020

CUDA Segmentation fault (core dumped) when calling rand() too much

I have a class named Layer, and its constructor (see below) will call the rand() function multiple times for initialization. When I initialize multiple Layer instances (see below) and each will call the rand() function, I got the Segmentation fault (core dumped) no matter using Nsight or command line.

static Layer l_input = Layer(0, 0, 255*255);
static Layer l_c1 = Layer(3*3, 4, 253*253*4);
static Layer l_s1 = Layer(3*3, 1, 126*126*4);
static Layer l_c2 = Layer(3*3, 16, 124*124*16);
static Layer l_s2 = Layer(3*3, 1, 61*61*16);
static Layer l_f = Layer(61*61*16, 150, 150);
static Layer l_f1 = Layer(150, 10, 10);


// Constructor
Layer::Layer(int M, int N, int O)
{
    this->M = M;
    this->N = N;
    this->O = O;
    float h_bias[N];
    float h_weight[N][M];
    for (int i = 0; i < N; i++) {
        h_bias[i] = 0.5f - float(rand()) / float(RAND_MAX);
        /*h_bias[i] = 0.0f;*/
        for (int j = 0; j < M; j++) {
            h_weight[i][j] = 0.5f - float(rand()) / float(RAND_MAX);
            //h_weight[i][j] = 1.f;
        }
    }

    cudaMalloc(&output, sizeof(float) * O);
    cudaMalloc(&preact, sizeof(float) * O);
    cudaMalloc(&bias, sizeof(float) * N);
    cudaMalloc(&weight, sizeof(float) * M * N);
    cudaMalloc(&d_output, sizeof(float) * O);
    cudaMalloc(&d_preact, sizeof(float) * O);
    cudaMalloc(&d_weight, sizeof(float) * M * N);
    cudaMemcpy(this->bias, h_bias, sizeof(float) * N, cudaMemcpyHostToDevice);
    cudaMemcpy(this->weight, h_weight, sizeof(float) * M * N, cudaMemcpyHostToDevice);
}



Aucun commentaire:

Enregistrer un commentaire