什么是一个有效的方式来增加多线程这个简单的算法? [英] What would be an efficient way to add multithreading to this simple algorithm?
问题描述
我会说我的C语言知识是公平的,我希望扩展计划,以提高我的并行编程方面的知识。
I would say my knowledge in C is fair, and I wish to extend a program to enhance my knowledge of parallel programming.
这基本上是程序我指的是一个强力发电机,以增加通过密码,如从0000 .. ZZZZ一个特定的字符集: 用蛮力$ C $下的crypt(3需要帮助)
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;
char 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是一个愚蠢的,如果简单的解决方案。我听说过的OpenMP的这在我的书看起来像一个很好的解决方案,您怎么看待这可以分头从我的电脑的多核受益?即核心1计算aaaa..ffff,和Core 2计算FFFF ... ZZZZ,这将使得这个意义上唯一的方法是什么?
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?
推荐答案
我觉得你回答你自己的问题。上线#2线#1和ffff..zzzz的aaaa..ffff可能是要走的路,只是也许把它分解成更threadable部分如果你有更多的内核可用。试图启动一个线程来执行DO循环的某些部分可能会推出比好处更多的开销在这样一个紧张的算法。
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.
这篇关于什么是一个有效的方式来增加多线程这个简单的算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!