Below is the code where i get Segmentation fault when i am trying to print the matrix d_A which is being copied from host matrix h_A.when i am trying to print matrix h_A just before cudamalloc it gets printed but after cudamemcpy trying to print d_A(Device matrix) gives me error.
I am using the following:- nvcc -arch=sm_20 Trial.cu -o out to compile
#include <stdio.h>
#include <sstream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdint.h>
#include <cuda.h>
#include <time.h>
inline void gpuAssert(cudaError_t code, char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
void LUdecomposition(float *h_A,float *A_,int dim,unsigned int size_A,int row_A)
{
float *d_A;int k;
gpuErrchk(cudaMalloc(&d_A, size_A*sizeof(float)));
gpuErrchk(cudaMemcpy(d_A, h_A, size_A*sizeof(float), cudaMemcpyHostToDevice));
printf("\n D_A");
gpuErrchk(cudaMemcpy(A_,d_A,size_A*sizeof(float), cudaMemcpyDeviceToHost));
for(int i=0; i<size_A; i++)
{
if (i % row_A == 0) printf("\n");
printf("%f ", A_[i]);
}
printf("\n D_A");
}
void input_matrix_generation_A(float *Matrix, unsigned int row, unsigned int column, unsigned int size)
{
for (int i=0; i<size; i++)
{
Matrix[i] = rand()%5+1;
if (i % column == 0) printf("\n");
}
}
int main(int argc, char *argv[])
{
int m=4;int dim=2;
int size_A=m*m;
float *A, *A_;
A = (float*)malloc(sizeof(float)*size_A);
input_matrix_generation_A(A,m,m,size_A);
A_ = (float*)malloc(sizeof(float)*size_A);
LUdecomposition(A,A_,dim,size_A,m);
for(int i=0; i<size_A; i++)
{
if (i % row_A == 0) printf("\n");
printf("%f ", A_[i]);
}
return 0;
}