我正在尝试解决C中用于查找MD5哈希字符串的问题。 为此,我想通过拆分程序代码将计算分配到不同的节点(2个)。 如何运行MPI并通过SSH服务器连接salve节点我已经从这个伟大的tut Specify the machines running program using MPI中学到了
是否有可能将一半for循环分开以在不同节点上计算? 当主节点或从节点首次运行时,程序在节点和出口上的工作方式相同,当我运行它时〜/ $ mpirun -n 2 -H master,slave1 myprogram
/*
C Example w/o mpi
mpicc md5.c -o md5 -lcrypto -lssl
gcc md5.c -o md5 -lcrypto -lssl
./md5
Single process on c2d laptop
String finding matchin md5 of string "hello" tooks
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>
#if defined(__APPLE__)
# define COMMON_DIGEST_FOR_OPENSSL
# include <CommonCrypto/CommonDigest.h>
# define SHA1 CC_SHA1
#else
# include <openssl/md5.h>
#endif
char *str2md5(const char *str, int length) {
int n;
MD5_CTX c;
unsigned char digest[16];
char *out = (char*)malloc(33);
MD5_Init(&c);
while (length > 0) {
if (length > 512) {
MD5_Update(&c, str, 512);
} else {
MD5_Update(&c, str, length);
}
length -= 512;
str += 512;
}
MD5_Final(digest, &c);
for (n = 0; n < 16; ++n) {
snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int)digest[n]);
}
return out;
}
int main (int argc, char* argv[])
{
#ifdef COUNT // Very bad name, not long enough, too general, etc..
static int const count = COUNT;
#else
static int const count = 6; // default value
#endif
clock_t begin, end;
double time_spent;
begin = clock();
int bflag = 0;
int sflag = 0;
int index;
int c;
char input[count];
char action[2]; // char + \n
char *inputResult = (char*)malloc(33);
char *tmpResult = (char*)malloc(33);
char inputGuess[6];
int i,j,k,l,m,n;
char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
printf("Please enter string to guess (max 6 char - latin letters and numbers): ");
scanf("%s", input);
inputResult = str2md5(input, strlen(input));
printf("Md5 to find = %s \n", inputResult);
printf("Continue? (Y/n) ");
scanf("%s", action);
if(action == "n\n"){
return 0;
}
/* for 1 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
char guess1[] = {letters[i], '\0'};
printf("%s\n", guess1);
if(strcmp(str2md5(guess1, strlen(guess1)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess1);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
/* for 2 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
char guess1[] = {letters[i], '\0'};
printf("%s\n", guess1);
if(strcmp(str2md5(guess1, strlen(guess1)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess1);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
int j;
for(j=0; j<sizeof(letters); j++){
char guess2[] = {letters[i], letters[j], '\0'};
printf("%s\n", guess2);
//tmpResult = str2md5(guess, strlen(guess));
if(strcmp(str2md5(guess2, strlen(guess2)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess2);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}
/* for 3 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
char guess1[] = {letters[i], '\0'};
printf("%s\n", guess1);
if(strcmp(str2md5(guess1, strlen(guess1)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess1);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
for(j=0; j<sizeof(letters); j++){
char guess2[] = {letters[i], letters[j], '\0'};
printf("%s\n", guess2);
//tmpResult = str2md5(guess, strlen(guess));
if(strcmp(str2md5(guess2, strlen(guess2)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess2);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
for(k=0; k<sizeof(letters); k++){
char guess3[] = {letters[i], letters[j], letters[k], '\0'};
printf("%s\n", guess3);
if(strcmp(str2md5(guess3, strlen(guess3)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess3);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}
}
/* for 4 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
char guess1[] = {letters[i], '\0'};
printf("%s\n", guess1);
if(strcmp(str2md5(guess1, strlen(guess1)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess1);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
for(j=0; j<sizeof(letters); j++){
char guess2[] = {letters[i], letters[j], '\0'};
printf("%s\n", guess2);
//tmpResult = str2md5(guess, strlen(guess));
if(strcmp(str2md5(guess2, strlen(guess2)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess2);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
for(k=0; k<sizeof(letters); k++){
char guess3[] = {letters[i], letters[j], letters[k], '\0'};
printf("%s\n", guess3);
if(strcmp(str2md5(guess3, strlen(guess3)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess3);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
for(m=0; m<sizeof(letters); m++){
char guess4[] = {letters[i], letters[j], letters[k], letters[m], '\0'};
printf("%s\n", guess4);
if(strcmp(str2md5(guess4, strlen(guess4)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess4);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}
}
}
/* for 5 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
char guess1[] = {letters[i], '\0'};
printf("%s\n", guess1);
if(strcmp(str2md5(guess1, strlen(guess1)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess1);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
for(j=0; j<sizeof(letters); j++){
char guess2[] = {letters[i], letters[j], '\0'};
printf("%s\n", guess2);
//tmpResult = str2md5(guess, strlen(guess));
if(strcmp(str2md5(guess2, strlen(guess2)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess2);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
for(k=0; k<sizeof(letters); k++){
char guess3[] = {letters[i], letters[j], letters[k], '\0'};
printf("%s\n", guess3);
if(strcmp(str2md5(guess3, strlen(guess3)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess3);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
for(m=0; m<sizeof(letters); m++){
char guess4[] = {letters[i], letters[j], letters[k], letters[m], '\0'};
printf("%s\n", guess4);
if(strcmp(str2md5(guess4, strlen(guess4)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess4);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
for(n=0; n<sizeof(letters); n++){
char guess5[] = {letters[i], letters[j], letters[k], letters[m], letters[n], '\0'};
printf("%s\n", guess5);
if(strcmp(str2md5(guess5, strlen(guess5)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess5);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}
}
}
}
/* for 6 char input */
for(i=0; i<sizeof(letters); i++){ //letters + numbers
char guess1[] = {letters[i], '\0'};
printf("%s\n", guess1);
if(strcmp(str2md5(guess1, strlen(guess1)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess1);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
for(j=0; j<sizeof(letters); j++){
char guess2[] = {letters[i], letters[j], '\0'};
printf("%s\n", guess2);
//tmpResult = str2md5(guess, strlen(guess));
if(strcmp(str2md5(guess2, strlen(guess2)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess2);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
for(k=0; k<sizeof(letters); k++){
char guess3[] = {letters[i], letters[j], letters[k], '\0'};
printf("%s\n", guess3);
if(strcmp(str2md5(guess3, strlen(guess3)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess3);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
for(m=0; m<sizeof(letters); m++){
char guess4[] = {letters[i], letters[j], letters[k], letters[m], '\0'};
printf("%s\n", guess4);
if(strcmp(str2md5(guess4, strlen(guess4)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess4);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
for(n=0; n<sizeof(letters); n++){
char guess5[] = {letters[i], letters[j], letters[k], letters[m], letters[n], '\0'};
printf("%s\n", guess5);
if(strcmp(str2md5(guess5, strlen(guess5)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess5);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
for(l=0; l<sizeof(letters); l++){
char guess6[] = {letters[i], letters[j], letters[k], letters[m], letters[n], letters[l], '\0'};
printf("%s\n", guess6);
if(strcmp(str2md5(guess6, strlen(guess6)),inputResult) == 0){
printf("We guessed it! Your input was - %s \n", guess6);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time spent: %f seconds\n", time_spent);
free(inputResult);
return 0;
}
}
}
}
}
}
}
return 0;
}
答案 0 :(得分:1)
这不是MPI的工作方式。在MPI中,您(大部分是炒作时间)编写一个在所有进程上运行的程序。在该程序中,您可以编写特定于行的代码,但是没有任何自动数据或任务并行性的构造。如果您希望某些进程运行循环的一部分而其他进程运行其他进程,则需要明确地将其写入您的程序。