Reading file in OpenCL program

Hi,
I have a very weird problem in an OpenCL project, in my program I read a text file at the host and load its contents to memory in order to start processing on it further, (this happens at the very beginning of the program and has nothing to do with GPU function calls; i.e it has nothing to do with parallelism)… I’m working on Visual Studio 2008,
when I call the ‘read’ built-in c++ function in ‘wifstream’, it takes very long time to finish reading the file! However when I create an empty project that reads the same file (not openCL project) it is read much quickly! I measured the file reading time in both programs (OpenCL program & my empty project) I found a huge gap! I’m really astonished from this behavior of the wifstream read function when called from an OpenCL project!!
any idea why this might happen?

Can’t anybody help me check why this strange behavior happens?!!
I made a small test to check if the problem is in my code: I copied one of the NVIDIA opencl samples and emptied the code written in it and pasted my code, and I created an empty project and pasted my code… the time taken by the new project to read a text file of size 40 MB was: 49 sec while the time taken to read the same file in the opencl copied sample was: 201 sec!!!

here’s a link of the file I read during this test:
http://www.4shared.com/file/fXpo_MvP/all.html

And also here’s my code if you want to try it yourself (plz don’t forget to change the directory path)…

#include "iostream"
#include "time.h"
#include <fstream>
#include <windows.h>
#include <string>
using namespace std;


ifstream::pos_type size;
void listDirFiles(wchar_t*, wstring*, int*);
void main()
{
	wstring filesList[23000];
	int filesNum=0;
	string Dir="H:\\++WORK\\ARABIC COLLECTIONS\\Articles\\uni2\\";
	listDirFiles(L"H:\\++WORK\\ARABIC COLLECTIONS\\Articles\\uni2\\*.txt",filesList, &filesNum);


	for(int i =0 ;i<filesNum;i++)
	{
		int len =filesList[i].length();
		char* fileName=(char*)malloc((len+1)*sizeof(char));
		wcstombs(fileName,filesList[i].c_str(),filesList[i].length());
		fileName[len]='\0';

		string fname=fileName;
		string fullPath=Dir+fname;


		wifstream file (fullPath.c_str(), ios::in|ios::ate);
		if (file.is_open())
		{  
			size = file.tellg();
			wchar_t * memblock = new wchar_t [size];
			file.seekg (0, ios::beg);
			unsigned t0 = clock();
			file.read (memblock, size);
			unsigned t1= clock();
			cout<<"Time to read file "<<i<< " is: "<<((double)(t1-10)/CLOCKS_PER_SEC)<<endl;
			file.close();
			
		}
	}
}
void listDirFiles(wchar_t* dirPath, wstring* fileName, int* filesNum)
{

	UINT counter(0);
	bool working(true);
	wstring buffer;

	WIN32_FIND_DATA myfile;

	HANDLE myHandle=FindFirstFile(dirPath,&myfile);

	if(myHandle!=INVALID_HANDLE_VALUE)
	{
		buffer=(wstring)(myfile.cFileName);
		fileName[counter]=buffer;
		filesNum[0]++;

		while(working)
		{
			FindNextFile(myHandle,&myfile);
			if(((wstring)myfile.cFileName).compare(buffer)!=0)
			{
				buffer=myfile.cFileName;
				++counter;
				fileName[counter]=buffer;
				filesNum[0]++;
			}
			else
			{
				//end of files reached
				working=false;
			}

		}
	}
}



sorry, forgot to mention that the file I’m testing upon is written in Arabic…

No idea yet as to why it may take longer, but at first glance, this line of code looks wrong:

cout<<"Time to read file "<<i<< " is: "<<((double)(t1-10)/CLOCKS_PER_SEC)<<endl;

I think that should be

cout<<"Time to read file "<<i<< " is: "<<((double)(t1-t0)/CLOCKS_PER_SEC)<<endl;

Check that line between both versions of your program.

OpenCL shouldn’t have anything / any interference with reading in a text file.

Thanks for the reply :slight_smile:
of course this was a mistake BUT still it behaves the same… but I tried working with ifstream instead of wifstream so it worked correctly and the reading time became almost the same in the two versions… I’m really astonished of the strange behavior with wiftream!