#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WORD_LENGTH 256
typedef struct wordNode wordNode;
struct wordNode
{
char word[WORD_LENGTH];
int iWordCount;
wordNode *pNext;
};
wordNode * map();
wordNode * reduce(wordNode * wordNode1,wordNode * wordNode2);
main( argc, argv )
int argc;
char **argv;
{
char word1[1024];
char word2[1024];
int wordcount1=0;
int wordcount2=0;
int myrank;
wordNode * pHeader1;
wordNode * pHeader2;
int count1=0;
int count2=0;
MPI_Status status;
MPI_Init(&argc, &argv );
MPI_Comm_rank(MPI_COMM_WORLD, &myrank );
if (myrank == 0)
{
pHeader1 = map();
wordNode * pH1;
pH1=pHeader1;
while(pH1!=NULL){
count1++;
pH1=pH1->pNext;
}
MPI_Send(&count1,1,MPI_INT,2,99,MPI_COMM_WORLD);
while(pHeader1!=NULL){
strcpy(word1,pHeader1->word);
wordcount1=pHeader1->iWordCount;
MPI_Send(word1, 32, MPI_CHAR,2,99,MPI_COMM_WORLD);
MPI_Send(&wordcount1,1,MPI_INT,2,99,MPI_COMM_WORLD);
pHeader1=pHeader1->pNext;
}
}else if(myrank==1)
{
pHeader2 = map();
wordNode * pH2;
pH2=pHeader2;
while(pH2!=NULL){
count2++;
pH2=pH2->pNext;
}
MPI_Send(&count2,1,MPI_INT,2,99,MPI_COMM_WORLD);
while(pHeader2!=NULL){
strcpy(word2,pHeader2->word);
wordcount2=(int)(pHeader2->iWordCount);
MPI_Send(word2, 32, MPI_CHAR,2,99,MPI_COMM_WORLD);
MPI_Send(&wordcount2, 1, MPI_INT,2,99,MPI_COMM_WORLD);
pHeader2=pHeader2->pNext;
}
}
else if(myrank==2)
{
int i=0;
int j=0;
wordNode * p1=(wordNode*)malloc(sizeof(wordNode));
wordNode * p2=(wordNode*)malloc(sizeof(wordNode));
wordNode * p1_return;
wordNode * p2_return;
p1_return=p1;
p2_return=p2;
MPI_Recv(&count1, 1, MPI_INT, 0, 99, MPI_COMM_WORLD,&status);
printf("******received********* : %d\n", count1);
MPI_Recv(&count2, 1, MPI_INT, 1, 99, MPI_COMM_WORLD,&status);
printf("******received********* : %d\n", count2);
while(i<count1){
MPI_Recv(word1, 32, MPI_CHAR, 0, 99, MPI_COMM_WORLD,&status);
MPI_Recv(&wordcount1, 1, MPI_INT, 0, 99, MPI_COMM_WORLD,&status);
printf("received from node01: %s\n", word1);
printf("received from node01: %d\n", wordcount1);
wordNode * tmp_p = (wordNode*)malloc(sizeof(wordNode));
strcpy(p1->word,word1);
p1->iWordCount=wordcount1;
//pp=p1; //return pp;
p1->pNext=tmp_p;
p1=tmp_p;
i++;
}
/*
while(p1_return->pNext!=NULL){
printf("\n");
printf("=================================\n");
printf("received : %s\n", p1_return->word);
printf("received : %d\n", p1_return->iWordCount);
printf("=================================\n");
printf("\n");
p1_return=p1_return->pNext;
}
*/
while(j<count2){
MPI_Recv(word2, 32, MPI_CHAR, 1, 99, MPI_COMM_WORLD,&status);
MPI_Recv(&wordcount2, 1, MPI_INT, 1, 99, MPI_COMM_WORLD,&status);
printf("received from node02: %s\n", word2);
printf("received from node02: %d\n", wordcount2);
wordNode * tmp_p02 = (wordNode*)malloc(sizeof(wordNode));
strcpy(p2->word,word2);
p2->iWordCount=wordcount2;
p2->pNext=tmp_p02;
p2=tmp_p02;
j++;
}
/*
while(p2_return->pNext!=NULL){
printf("\n");
printf("=================================\n");
printf("received : %s\n", p2_return->word);
printf("received : %d\n", p2_return->iWordCount);
printf("=================================\n");
printf("\n");
p2_return=p2_return->pNext;
}
*/
wordNode * pHeader = reduce(p1_return,p2_return);
while(pHeader!=NULL){
printf("\n");
printf("=================================\n");
printf("received : %s\n", pHeader->word);
printf("received : %d\n", pHeader->iWordCount);
printf("=================================\n");
printf("\n");
pHeader=pHeader->pNext;
}
}
MPI_Finalize();
}
wordNode * map(){
wordNode *pHeader = NULL;
char temp[WORD_LENGTH];
//打开要读取的文件
FILE *fp;
if( NULL == (fp=fopen("123.txt", "r")) )
{
printf("Open file failed!!\n");
exit(1);
}
//循环读取文本中的内容
while( EOF != (fscanf(fp,"%s",temp)) )
{
//CountWord(temp);
wordNode *pNode = NULL;
if( NULL == pHeader)
{
pHeader = (wordNode*)malloc(sizeof(wordNode));
strcpy(pHeader->word, temp);
pHeader->iWordCount = 0;
pHeader->pNext = NULL;
pNode = pHeader;
}
//搜索现有的链表
else{
wordNode *pCurr = pHeader;
wordNode *pPre = NULL;
while( (NULL != pCurr) && (0 != strcmp(pCurr->word, temp)) )
{
pPre = pCurr;
pCurr = pCurr->pNext;
}
//该单词不存在
if(NULL == pCurr)
{
pCurr = (wordNode*)malloc(sizeof(wordNode));
strcpy(pCurr->word, temp);
pCurr->iWordCount = 0;
pCurr->pNext = NULL;
pPre->pNext = pCurr;
}
pNode = pCurr;
}
if(NULL == pNode)
{
continue;
}
else
{
pNode->iWordCount++;
}
}
return pHeader;
}
wordNode * reduce(wordNode * wordNode1,wordNode * wordNode2){
wordNode * temp1 = NULL;
wordNode * temp2 = NULL;
if(wordNode1 == NULL){
return wordNode2;
}
else{
temp1 = wordNode1;
temp2 = wordNode2;
while(temp1 != NULL){
int flag=0;
while(temp2!= NULL)
{
if(strcmp(temp1->word, temp2->word)==0){
temp2->iWordCount += temp1->iWordCount;
flag=1;
break;
}
else
temp2 = temp2->pNext;
}
if(!flag){
wordNode *dd = (wordNode*)malloc(sizeof(wordNode));
strcpy(dd->word, temp1->word);
dd->iWordCount=temp1->iWordCount;
dd->pNext = NULL;
temp2->pNext = dd;
}
temp2 = wordNode2;
temp1 = temp1->pNext;
}
}
return temp2;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WORD_LENGTH 256
typedef struct wordNode wordNode;
struct wordNode
{
char word[WORD_LENGTH];
int iWordCount;
wordNode *pNext;
};
wordNode * map();
wordNode * reduce(wordNode * wordNode1,wordNode * wordNode2);
main( argc, argv )
int argc;
char **argv;
{
char word1[1024];
char word2[1024];
int wordcount1=0;
int wordcount2=0;
int myrank;
wordNode * pHeader1;
wordNode * pHeader2;
int count1=0;
int count2=0;
MPI_Status status;
MPI_Init(&argc, &argv );
MPI_Comm_rank(MPI_COMM_WORLD, &myrank );
if (myrank == 0)
{
pHeader1 = map();
wordNode * pH1;
pH1=pHeader1;
while(pH1!=NULL){
count1++;
pH1=pH1->pNext;
}
MPI_Send(&count1,1,MPI_INT,2,99,MPI_COMM_WORLD);
while(pHeader1!=NULL){
strcpy(word1,pHeader1->word);
wordcount1=pHeader1->iWordCount;
MPI_Send(word1, 32, MPI_CHAR,2,99,MPI_COMM_WORLD);
MPI_Send(&wordcount1,1,MPI_INT,2,99,MPI_COMM_WORLD);
pHeader1=pHeader1->pNext;
}
}else if(myrank==1)
{
pHeader2 = map();
wordNode * pH2;
pH2=pHeader2;
while(pH2!=NULL){
count2++;
pH2=pH2->pNext;
}
MPI_Send(&count2,1,MPI_INT,2,99,MPI_COMM_WORLD);
while(pHeader2!=NULL){
strcpy(word2,pHeader2->word);
wordcount2=(int)(pHeader2->iWordCount);
MPI_Send(word2, 32, MPI_CHAR,2,99,MPI_COMM_WORLD);
MPI_Send(&wordcount2, 1, MPI_INT,2,99,MPI_COMM_WORLD);
pHeader2=pHeader2->pNext;
}
}
else if(myrank==2)
{
int i=0;
int j=0;
wordNode * p1=(wordNode*)malloc(sizeof(wordNode));
wordNode * p2=(wordNode*)malloc(sizeof(wordNode));
wordNode * p1_return;
wordNode * p2_return;
p1_return=p1;
p2_return=p2;
MPI_Recv(&count1, 1, MPI_INT, 0, 99, MPI_COMM_WORLD,&status);
printf("******received********* : %d\n", count1);
MPI_Recv(&count2, 1, MPI_INT, 1, 99, MPI_COMM_WORLD,&status);
printf("******received********* : %d\n", count2);
while(i<count1){
MPI_Recv(word1, 32, MPI_CHAR, 0, 99, MPI_COMM_WORLD,&status);
MPI_Recv(&wordcount1, 1, MPI_INT, 0, 99, MPI_COMM_WORLD,&status);
printf("received from node01: %s\n", word1);
printf("received from node01: %d\n", wordcount1);
wordNode * tmp_p = (wordNode*)malloc(sizeof(wordNode));
strcpy(p1->word,word1);
p1->iWordCount=wordcount1;
//pp=p1; //return pp;
p1->pNext=tmp_p;
p1=tmp_p;
i++;
}
/*
while(p1_return->pNext!=NULL){
printf("\n");
printf("=================================\n");
printf("received : %s\n", p1_return->word);
printf("received : %d\n", p1_return->iWordCount);
printf("=================================\n");
printf("\n");
p1_return=p1_return->pNext;
}
*/
while(j<count2){
MPI_Recv(word2, 32, MPI_CHAR, 1, 99, MPI_COMM_WORLD,&status);
MPI_Recv(&wordcount2, 1, MPI_INT, 1, 99, MPI_COMM_WORLD,&status);
printf("received from node02: %s\n", word2);
printf("received from node02: %d\n", wordcount2);
wordNode * tmp_p02 = (wordNode*)malloc(sizeof(wordNode));
strcpy(p2->word,word2);
p2->iWordCount=wordcount2;
p2->pNext=tmp_p02;
p2=tmp_p02;
j++;
}
/*
while(p2_return->pNext!=NULL){
printf("\n");
printf("=================================\n");
printf("received : %s\n", p2_return->word);
printf("received : %d\n", p2_return->iWordCount);
printf("=================================\n");
printf("\n");
p2_return=p2_return->pNext;
}
*/
wordNode * pHeader = reduce(p1_return,p2_return);
while(pHeader!=NULL){
printf("\n");
printf("=================================\n");
printf("received : %s\n", pHeader->word);
printf("received : %d\n", pHeader->iWordCount);
printf("=================================\n");
printf("\n");
pHeader=pHeader->pNext;
}
}
MPI_Finalize();
}
wordNode * map(){
wordNode *pHeader = NULL;
char temp[WORD_LENGTH];
//打开要读取的文件
FILE *fp;
if( NULL == (fp=fopen("123.txt", "r")) )
{
printf("Open file failed!!\n");
exit(1);
}
//循环读取文本中的内容
while( EOF != (fscanf(fp,"%s",temp)) )
{
//CountWord(temp);
wordNode *pNode = NULL;
if( NULL == pHeader)
{
pHeader = (wordNode*)malloc(sizeof(wordNode));
strcpy(pHeader->word, temp);
pHeader->iWordCount = 0;
pHeader->pNext = NULL;
pNode = pHeader;
}
//搜索现有的链表
else{
wordNode *pCurr = pHeader;
wordNode *pPre = NULL;
while( (NULL != pCurr) && (0 != strcmp(pCurr->word, temp)) )
{
pPre = pCurr;
pCurr = pCurr->pNext;
}
//该单词不存在
if(NULL == pCurr)
{
pCurr = (wordNode*)malloc(sizeof(wordNode));
strcpy(pCurr->word, temp);
pCurr->iWordCount = 0;
pCurr->pNext = NULL;
pPre->pNext = pCurr;
}
pNode = pCurr;
}
if(NULL == pNode)
{
continue;
}
else
{
pNode->iWordCount++;
}
}
return pHeader;
}
wordNode * reduce(wordNode * wordNode1,wordNode * wordNode2){
wordNode * temp1 = NULL;
wordNode * temp2 = NULL;
if(wordNode1 == NULL){
return wordNode2;
}
else{
temp1 = wordNode1;
temp2 = wordNode2;
while(temp1 != NULL){
int flag=0;
while(temp2!= NULL)
{
if(strcmp(temp1->word, temp2->word)==0){
temp2->iWordCount += temp1->iWordCount;
flag=1;
break;
}
else
temp2 = temp2->pNext;
}
if(!flag){
wordNode *dd = (wordNode*)malloc(sizeof(wordNode));
strcpy(dd->word, temp1->word);
dd->iWordCount=temp1->iWordCount;
dd->pNext = NULL;
temp2->pNext = dd;
}
temp2 = wordNode2;
temp1 = temp1->pNext;
}
}
return temp2;
}