0

I implemented a representation of screen with some number of pixels that can draw a simple pyramid (by assigning an RBG value for each pixel in the screen). While learning CUDA C, I wanted to re-implement it to assign these values in the GPU, but it was complicated for me to allocate memory properly for a struct of structs.

I already tried this on the CPU :

#include <stdlib.h>
#include <stdio.h>
typedef struct {
unsigned char r;
unsigned char g;
unsigned char b;
} Pixel;

typedef struct {
    unsigned int width;
    unsigned int height;
    Pixel* pxp ;
} Screen;
Screen* newScreen(int width, int height){
    Screen *sc=malloc(sizeof(Screen));

    sc->width=width;
    sc->height=height;
    sc->pxp=malloc(width*height*sizeof(Pixel));

    return sc;
}
void removeScreen(Screen* sc){
    free(sc->pxp);
    free(sc);
}
void drawPrmd(Screen *sc){
    for(long int i=0;i<sc->height;i++){
        int index=(int) (i*sc->width +sc->width/2);


        for(long int j=index;j>=index-i;j--){
            if((j< (sc->height*sc->width))&&(j>0)){
                sc->pxp[j].r=255;
                sc->pxp[j].b=255;
                sc->pxp[j].g=255;
            }
        }
        for(long int j=index;j<=index+i;j++){
            if((j< (sc->height*sc->width))&&(j>0)){
            sc->pxp[j].r=255;
            sc->pxp[j].b=255;
            sc->pxp[j].g=255;
            }
        }
    }
}
void printScreen(Screen * sc){
    for(int i=0;i<sc->height;i++){
        for(int j=0;j<sc->width;j++){
            printf("(%u) \t",sc->pxp[i*sc->width+j].r);
        }
        printf("\n");
    }
}

int main(){
    Screen *sc1=newScreen(80,80);
    drawPrmd(sc1);
    printScreen(sc1);
    removeScreen(sc1);

}

when tried to implement it to work on the GPU, I had a problem with allocating GPU memory :

Screen* newScreenGPU(int width, int height){

    Screen *sc;
    if(cudaMalloc((void **) &sc,sizeof(Screen))!=cudaSuccess)
        printf("Error allocating GPU memory for screen \n");

    if(cudaMalloc((void **) sc->pxp,width * height*sizeof(Pixel))!=cudaSuccess)
        printf("Error allocating GPU memory for pixels \n");
    return sc;
}

**it compiles smoothly but I get Segmentation fault (core dumped) exactly at memory allocation for pixels (second allocation).

-Expected: allocating memory and assigning RGB values for pixels. -Actual result: Segmentation fault (core dumped).

SellaDev
  • 109
  • 5
  • 1
    sc->pxp is a pointer to device memory. It is illegal to dereference it in host memory. This is an extremely common question and there are lots of examples of how to do this correctly if you care to search for them – talonmies Apr 15 '19 at 05:49
  • I thought so, as I read using the arrow like in sc->pxp means de-referencing but I tried to do `sc.pxp` and it gives the following error : `screenGPU.cu(54): error: expression must have class type ` . would you please show me the appropriate way to do it ? – SellaDev Apr 15 '19 at 06:03
  • PS: `&sc->pxp` alos gives `Segmentation fault (core dumped)` – SellaDev Apr 15 '19 at 06:12
  • I had the same problem, see the link for explanation and solution. – Ander Biguri Apr 15 '19 at 08:29

0 Answers0