Need help with counting within parallelized block - OpenMP/C-Programming
Hello together,
I have a question for you. Since a few weeks I am trying to programm
a small bruteforce application in C on Ubuntu 14.04.1 using Code::Blocks
with gcc-4.8.2. My programm is nothing special. It's just for practise,
Without hashing or something like that. For me the focus is on parallelize
the procedure that generates words. I'm using OpenMP for this small project.
Until now it works good for me. There is just one thing that I would like to
implement, but without success so far.
I want that my programm counts all the generated words, regardless of wether
the search string was found or not. I hope you understand what I'm talking
about. I'm sorry for my bad english. I am from germany.
I declared a global unsigned integer variable with the name 'Counter' for counting all
the generated words.
Because the whole procedure is parallelized, the variable is specified as reduction
variable.
So the problem is, that the reduction is not completed until the end of the parallel region,
when the search string was found.
That means only when my programm doesn't have a match, at the end the number of counts for
all generated words is correct. Why? Because when nothing was found, the application reaches
the end of the parallel region and the reduction is completed.
But if my programm has a match, the number of counts is always wrong at the output.
Now, I would like to know, if there is way to exit sane and portable from a parallel region,
without have to wait until all possibilites of words has been tried? Do you understand
what I mean? I.e. we are searching the following string: "4788". I tell my programm the max.
length of words should be 4. From "0" until "9999" altogether are 11110 tries, using just
digits (0123456789) for the brute force procedure. If the word is found, the number of tries (counts)
is 5899.
Is it possible to exit sane and portable from a parallel region earlier?
I.e. when the search string was found? By the same behavior like if nothing was found? So that at the end
the number of counts will displayed correct, even if the word was found?
I have tried something with two variables 'CheckStatus' and 'MyCheckStatus'. This variables should indicate
when work is done (i.e. if there's a match).
With omp atomic read and omp atomic write. But unfortunately without success.
Here is my source code:
Without parallelization I have this output:
That is correct. But with parallelization the output is as follows:
The goal is, that the number of counts is always correct with parallelization.
Please could you help me with this issue? If possible with small code examples or improving my source code, too. I would be very very appreciated.
DaveX
Last edited by DaveX; 10-10-2014 at 02:54 AM..
Reason: Change PHP tags to CODE tags.
The count isn't wrong so to speak. If it launches 12 threads at once, the 1st of which finds your value, that doesn't mean the other 11 never ran.
Yes, I know this. But this is not the problem.
As I already wrote, the problem is that I would like to know, how to reach that even though the programm has a match, it shows the correct number of counts. In order that it works, I have to find a way the application exits the parallelized block once the word is found. But with the same behavior as it'd not find anything. Because how I already explained, the correct number of counts is displayed only if the end of the parallelized block was reached. Only thus the reduction can complete.
My point is, count isn't wrong as much as completely meaningless. The order your threads run in isn't absolute. Thread #5 could run before thread #1, succeed, and return a count of 1! If you want the threads to know what order they are, you'll have to tell them.
Increment outside the parallel section and feed that value into the function as a parameter. If the function succeeds, have it set a flag variable to the correct 'count' value. Other threads can check that flag variable before their loop, and quit early if nonzero.
Last edited by Corona688; 10-10-2014 at 12:46 PM..
My point is, count isn't wrong as much as completely meaningless. The order your threads run in isn't absolute. Thread #5 could run before thread #1, succeed, and return a count of 1!
I know. That's the reason why I specified 'Counter' as a reduction variable. But you're right. My implementation doesn't do what I expect. So I would try to implement your suggestions.
Quote:
Originally Posted by Corona688
If you want the threads to know what order they are, you'll have to tell them.
Could you show me how to do this? Maybe a small code example?
Quote:
Originally Posted by Corona688
Increment outside the parallel section and feed that value into the function as a parameter. If the function succeeds, have it set a flag variable to the correct 'count' value. Other threads can check that flag variable before their loop, and quit early if nonzero.
I will try to do this. Could you help me? Maybe I would understand it better if you could give a small example. If you want?
I don't understand this multithreading library in particular, just multithreading in general, so I can't give you OMP code, but I can show you the concept of what I mean:
You can run that in parallel and not have 5000 "wrong" threads upsetting the value of 'flag', since it only gets set on success. And order is irrelevant since each thread is told exactly what its number is.
The size of an integer limits the number of unique digit strings you can create, but that was also true before. (i.e. a 30-digit string has 10^30 possibilities, an integer only holds up to 2^31) You could use more complicated things than a single int -- like an array of integers, one for each digit -- but the principle remains the same.
Last edited by Corona688; 10-10-2014 at 01:54 PM..
Cool. Thank you very much. Now I know what you mean. I will try to implement it in my source code. That is a really good idea. Thanks a lot. Later I will post my modified source.
hi,
I have two basic questions, I will be really grateful if I receive any
comment from you,
I have an MPI code where I am trying to implement OpenMP directives.
The machine where I run the code has 16 cores. I run the code with
export OMP_NUM_THREADS=2
mpirun -np 4 ./exec
If I... (0 Replies)
I am using a large code-base that compiled successfully before using make with a makefile and cmake. However, now that I'm trying to use openmp with it, I'm now getting the errors
undefined reference to `omp_get_thread_num'
undefined reference to `omp_get_num_threads'I don't think this... (0 Replies)
Dear Linux users,
I'm a noob at openmp, gcc and c programming. I can run my own openmp code in terminal with no problem, eg. gcc -fopenmp program.c -o program.
But now I'm trying to compile and run another person's code, it contains a makefile and multiple .c and .h files. I don't know how to... (2 Replies)
Hello to all,
Here is my situation. Some time in the mid-80's I stumbled across a small white programming book - can't remember the name but it was unique in that it started right out giving instructions on creating building blocks in code as a foundation for a complete system. The book was... (2 Replies)
Hi folks,
I am trying to run more than 8 threads in OpenMP team on my HP-UX 11i v3 system (without root access), but NO success.
Compiler: aCC A.06.26
I tried to setup: OMP_NUM_THREADS, omp_set_num_threads(), max_thread_proc=1000, nkthread=8416, set_dynamic=0
Machine has 2 processors... (1 Reply)
hello
im new here so i want to say hi everybody :)
i have to write a script and im newbie :/ i hope that in this forum are many ppl who knows subject :)
i have hundrets folders. in each folder is a file name trace.txt. each trace.txt has a lot of tracert's results separates with "-----" it... (6 Replies)
Hello everyone,
I've got a question regarding the gzip command.
I regulary use gzip to pack huge ammounts of files.
Is it ok to start 'gzip *' several times in the same directory to parallelize the packing process or can this result in problems, e.g. broken or unpacked files?
My tests... (7 Replies)
Hello,
I am trying to learn Networking Programming in C in unix enviorment. I want to know how good it is to become a network programmer. i am crazy about Network programming but i also want to opt for the best carreer options. Anybody experienced Network Programmer, please tell me is my... (5 Replies)