I have a piece of code that reads a file line by line and then it stores two corresponding binary representations in two vectors. But the size of vectors and the total number of lines processed are zero.
int numLines = 319426908; // calculated before for loop
char temp[100];
vector<long long int> p1, p2;
long long int c = 0;
#pragma omp parallel for schedule(dynamic) shared(c, p1, p2, fp) private(temp)
for(int i=0; i<numLines; i++){
if(fgets(temp, 100, fp) != NULL){
temp[strlen(temp)-1] = '\0';
long long int *A = toLongLong(temp);
p1.push_back(A[0]);
p2.push_back(A[1]);
c++;
}
}
cout << "Completed ...c = " << c << endl;
cout << "p1.size: " << p1.size() << " p2.size: " << p2.size() << endl;
This is the output
Completed ...c = 0
p1.size: 0 p2.size: 0
Where am I going wrong in the above piece of code?
push_backoperations as well as thec++(which should be++c) are critical and have to go into a critical section. But reading from a file in parallel is usually not a good idea (there are file formats which are designed to be written and read in parallel such as HDF5).sharedclause take care of that? should I use#pragma omp criticalfor the block where I am updatingp1,p2, andc?sharedmerely states explicitly that a variable is shared between threads. Synchronisation has to be handled by the user. Yes, you have to use a critical section but this will kill all the speedup you are expecting to get.#pragma omp critical. My input file is a txt file containing strings of fixed size in each line. The number of lines is almost 300 millions