0
#include <stdio.h>
#include <sys/time.h>

#include <cuda_runtime.h>

float *h_A, *h_B, *h_C, *d_A, *d_B, *d_C;
float **d_Many, **h_Many;
cudaError_t err = cudaSuccess;
long numElements = 10000000;
double startHostAllocate, endHostAllocate, startDeviceAllocate,
       endDeviceAllocate, startCopy, endCopy, startExecute, endExecute;

double cpuSecond() {
    struct timeval tp;
    gettimeofday(&tp, NULL);
    return ((double) tp.tv_sec + (double) tp.tv_usec * 1.e-6);
}

void** allocateManyHostMemory(void **manyHostMemory, int length, size_t size,
        int numElements) {
    manyHostMemory = (void **) malloc(sizeof(void*) * length);
    printf("Host array memory allocated");
    for (int i = 0; i < length; i++) {
        manyHostMemory[i] = malloc(size * numElements);
    }
    return manyHostMemory;
}

void allocateMemory(int numElements) {
    bool memcpyThisArray[numElements];

    startHostAllocate = cpuSecond();
    {
        allocateManyHostMemory((void **) h_Many, 3, sizeof(float), numElements);
    }
    endHostAllocate = cpuSecond();
    printf("Host memory allocated");
}

int main(void) {
    startDeviceAllocate = cpuSecond();
    allocateMemory(numElements);
    endDeviceAllocate = cpuSecond();
}

Edit gdb results

  Program received signal SIGSEGV, Segmentation fault.
allocateMemory (numElements=10000000) at addOperation.cu:46
46      startHostAllocate = cpuSecond();
(gdb) bt
#0  allocateMemory (numElements=10000000) at addOperation.cu:46
#1  0x00000000004027f9 in main () at addOperation.cu:59
(gdb) 

what am I missing here ?

Edit Again for MVCE I have added code so that it can be copied and compiled.

Community
  • 1
  • 1
Siddharth
  • 9,349
  • 16
  • 86
  • 148
  • now what ? why downvote.. I learnt gdb and came back.. – Siddharth Dec 05 '15 at 18:10
  • 2
    Provide an [MCVE](http://stackoverflow.com/help/mcve). It's [expected](http://stackoverflow.com/help/on-topic) for questions like this. – Robert Crovella Dec 05 '15 at 18:32
  • 2
    This has nothing to do with CUDA and the segmentation fault appears to be happening on code you haven't even shown us. I'm not sure how you imagine we could help you – talonmies Dec 05 '15 at 18:58
  • the line where the segmentation fault shows is mentioned, so is the code.. the gdb too shows the line number.. seriously what am I missing here.. im a SO user and dont intend to have a bad question.. – Siddharth Dec 06 '15 at 04:32
  • 1
    Read the MCVE link I provided. Pay particular attention to the letter C. What does C stand for in the context of MCVE? – Robert Crovella Dec 06 '15 at 05:13
  • thanks for your patience guys.. here goes.. please fix the vote and remove the "close" vote if you think its good now.. – Siddharth Dec 06 '15 at 05:27
  • @Siddharth: There is *no* CUDA code in your example at all. Why have you framed this as a CUDA question? – talonmies Dec 06 '15 at 08:15
  • This is only the first part of my code, after this I allocate device memory and do my operations. Until this works I cant move ahead.. – Siddharth Dec 06 '15 at 08:27
  • Gentlemen, the question is correctly formed now. Deserves to be on SO, please fix the votes. – Siddharth Dec 06 '15 at 08:36
  • 1
    Actually it deserves to be closed as a duplicate of the question I linked to in my answer http://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes (or another like it) . I would invoke [Mjölnir](http://meta.stackoverflow.com/questions/268608/where-did-the-use-of-the-name-mjolnir-come-from) but I have already close voted on this question, and I am leaving it that way. – talonmies Dec 06 '15 at 08:51

1 Answers1

3

The problem is the same as this (and nothing to do with CUDA):

bool memcpyThisArray[numElements];

when numElements=10000000, your program runs out of heap space and produces a stack overflow/segmentation fault. Change the code to something like:

   void allocateMemory(int numElements) {
        /* bool memcpyThisArray[numElements]; */

        startHostAllocate = cpuSecond();
        {
            allocateManyHostMemory((void **) h_Many, 3, sizeof(float), numElements);
        }
        endHostAllocate = cpuSecond();
        printf("Host memory allocated");
    }

and the problem will go away.

If you actually need to use memcpyThisArray for something in your real application, dynamically allocate it.

Community
  • 1
  • 1
talonmies
  • 70,661
  • 34
  • 192
  • 269