我正在从输入文件中进行关于字数统计的课程作业。我的所有同学都使用mpi_send
和mpi_recv
进行此分配。
虽然我仍然想知道mpi_reduce
是否真的不能在这个特定的任务中使用,因为我们已经尝试了但是找不到确切的解决方案。此代码使用hashmap和arraylist。以下是代码:
//split each line into word-by-word and count the occurrences
for(int i=0; i < inline.size(); i++)
{
//to read line by line
String input = inline.get(i);
//to split the string on whitespace using regular expression "\\s+"
String[] words = input.split("\\s+");
for(int j =0;j<words.length;j++) {
//start counting the occurences
for (String word : words)
{
wordMap.put(word,
(wordMap.get(word) == null ? 1 : (wordMap.get(word) + 1)));
}
localSum[0]+= words[j];
}
}
int [] globalSum = new int [1];
//using reduce method
MPI.COMM_WORLD.Reduce(localSum, 0, globalSum, 0, localSum.length, MPI.OBJECT, MPI.PROD, 0);
//for time taken
toc=MPI.Wtime();
//to ensure all process are completed so that it can proceed to next step
MPI.COMM_WORLD.Barrier();
//to display the result at rank 0
if (rank == 0)
{
//display the result
System.out.println("The results :");
for (String name: wordMap.keySet()){
String key =name.toString();
String value = wordMap.get(name).toString();
System.out.println("I am process "+ rank +" "+ key + " = " + value);
}
//display the time
System.out.println("The time takens :"+ (toc-tic)/100);