Open MP

I have not so much experience with Open MP. I just want to ask if my idea is good or not.

What I have to do is to make parallel a string matching algorithm where every thread takes a text from txt file.
I have the txt. file where in each line
I have a text, which I have to use in my algorithm.

My idea is to count how many lines in my txt file there are.After that I will divide the lines in blocks for each thread

for example:
I have 4 threads , my txt datei has 16 lines,
thread Number 1 takes line 1 to line 4,
thread Number 2 line 5 to line 8,
thread Number 3 line 9 to 12,
thread Number 4 from 13 to 16

Is it the best approach or I can do it faster?
Last edited on
You are trying to speed up opening a file by multi-threading the reading. Am I right?

Here are some things to consider:
1. Multi-threading speeds up processing by making use of multiple CPUs.
2. You can only speed up a process by working with the "bottle-neck".
3. The bottleneck in reading a file, is not in the processing of that file, but in the transfer of data from the hard-drive.

Multi-threading your reading of the file will not speed up your function. On the contrary, the fastest way to read a hard-drive is continuously without jumping from address to address. This is because the head which moves from track to track has to physically move. If you multi-thread, then the hard-drive will respond to each thread individually and will likely not read the file in sequence. This means that it will need to jump from address to address which physically takes time.

The fastest way to read a file, is to do it upon load-time of your program when is initializing, and then read it from memory, not from the hard-drive.
Stewbond, thank you for the answer!
My idea:
I have already written a function which read a txt file where my text is which I will use in the algorithm.
Here my code:
string text;
	string t;
	ifstream Text(argv[1],ios::in);
    if(!Text) //Always test the file open.
        cout<<"Error opening output file"<<endl;
        return -1;
    while (getline(Text, t))
	for (unsigned int i=0;i<t.length();++i){

The idea is a multi-threading of small textparts which are in every line of other txt file. Every small textpart I want to send to a thread and this thread takes the whole text which I have copied from txt file in the variable text.Every thread takes a small textpart too and does my algorithm.
As far as I understood I have to read the txt file line by line and save it in memory for example my idea is to save it in vector of strings? Have i understood it correct?
Topic archived. No new replies allowed.