CUDA Segmentation Fault CUDA File Handling Issues

#include<stdio.h>

int main ()

{

		

	

	FILE *fp;

	

 	fp = fopen("3GWC.txt","r");

	char sentence[20];

	int count=0; 	

	while (feof(fp)!=1)

	  {

		fgets(sentence, 20, fp);

		count++;

	}

	  

	printf("Value of Count:%d\n",count);

	  

}

This code gives me a segmentation fault when run using the nvidia c compiler. I ran the same code using gcc (ofcourse after changing the file extention to .c) and it ran perfectly. The file I used 3GWC.pdb is attached with this post.

I also ran valgrind to check memory errors. Valgrind pointed out that there is an error with the fgets statement. Kindly try this code on your nvccompiler and see whether it runs. Do share the output with me.

Thanks

Gitimo
3GWC.txt (1.28 MB)

Maybe you should check if the file is actually open first.

if (fp==NULL) perror (“Error opening file”);

N.

You never use fgets.
Program will never finish.
Might wanna repost your code.

Oh yeah, Sorry. I edited the code to add those lines… This edited code doesnt work.

Regarding the second post from top - The file is getting opened.

gitimo

Works fine on my system:

Value of Count:83103

Process returned 0 (0x0) execution time : 0.000 s

Press ENTER to continue.

N.

Please save this code as a .cu file and then run it on your Nvidia C Compiler and not Gcc. Is it not throwing up errors?? I can run this code without errors in gcc but not on nvcc.

G

I did…

/usr/local/cuda/bin/nvcc -o obj/Release/src/test.cu.o -c src/test.cu --keep -arch=sm_11 --ptxas-options=-v --compiler-options -fno-strict-aliasing -I./inc -I/usr/local/cuda/include -I/home/ncornelis/NVIDIA_CUDA_SDK/common/inc -DUNIX -DUSE_CUDA -O3

ptxas info : Compiling entry function ‘dummy_entry

ptxas info : Used 0 registers

g++ -fPIC -o bin/Release/test obj/Release/src/test.cu.o -L/usr/local/cuda/lib -L/home/ncornelis/NVIDIA_CUDA_SDK/lib -L/home/ncornelis/NVIDIA_CUDA_SDK/common/lib -lcuda -lcudart -lcutil -lcublas -lpthread

Value of Count:29582

Process returned 0 (0x0) execution time : 0.000 s

Press ENTER to continue.

N.

me too.

@:~$ cat 3gwc.cu

#include<stdio.h>

int main ()

{

FILE *fp;

fp = fopen("3GWC.txt","r");

char sentence[20];

int count=0;     

while (feof(fp)!=1)

  {

    fgets(sentence, 20, fp);

    count++;

}

printf("Value of Count:%d\n",count);

}

@:~$ nvcc 3gwc.cu -o 3gwc

@:~$ ls -l 3gwc* 3GWC*

-rw-r–r-- 1 ***** ***** 1346250 2009-07-10 09:42 3GWC.txt

-rwxr-xr-x 1 ***** ***** 82472 2009-07-10 09:45 3gwc

-rw-r–r-- 1 ***** ***** 254 2009-07-10 09:44 3gwc.cu

-rw-r–r-- 1 ***** ***** 135 2009-07-10 09:45 3gwc.linkinfo

@:~$ ./3gwc

Value of Count:83103

@:~$

@:~$ nvcc --version

nvcc: NVIDIA ® Cuda compiler driver

Copyright © 2005-2009 NVIDIA Corporation

Built on Thu_Apr__9_07:37:20_PDT_2009

Cuda compilation tools, release 2.2, V0.2.1221

@:~$