What would be an efficient way to add multithreading to this simple algorithm?
I would say my knowledge in C is fair, and I wish to extend a program to enhance my knowledge of parallel programming.
It essentially the program I am refering to is a brute force generator, to increment through passwords such as from 0000 .. zzzz of a specific character set: Need help with brute force code for crypt(3)
The algorithm is outlined below (credit to Jerome for this)
int len = 3;
cha开发者_JS百科r letters[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int nbletters = sizeof(letters)-1;
int main() {
int i, entry[len];
for(i=0 ; i<len ; i++) entry[i] = 0;
do {
for(i=0 ; i<len ; i++) putchar(letters[entry[i]]);
putchar('\n');
for(i=0 ; i<len && ++entry[i] == nbletters; i++) entry[i] = 0;
} while(i<len);
}
In what logical way would you say this could be extended by multithreading?
CUDA is a silly, if simple, solution. I had heard of OpenMP which in my books looks like a good solution, how do you think this could be split up to benefit from multiple cores of my computer? I.e. core 1 computing aaaa..ffff, and core 2 computing ffff...zzzz, is this the only method that would make sense with this?
I think you answered your own question. The aaaa..ffff on thread #1 and ffff..zzzz on thread #2 is probably the way to go, except to maybe break it down into more threadable parts in case you have more cores available. Trying to start a thread to perform some part of the do loop would probably introduce more overhead than benefit in such a tight algorithm.
I assume that you want to see your output characters in the order they are referenced in the entry
array.
This is a sequential operation you can not parallelize it.
Edit:
OK, now I see how wrong my was are :) You actually CAN parallelize this program, but you have to implement an additional layer handling the order of letters in the output. Also need to implement synchronization.
精彩评论