直接插入排序寫成序列版本只要十幾行,改成平行版就完全是另一回事了。這裡記錄一次以 MPI 為基礎的實作思路與完整程式碼。
#include "mpi.h"
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define END 0
#define INIT 1
#define DATA 2
#define ROOT 0
int readValue(char* file, int* values)
{
int fd, size, count = 0;
char buffer[80], num[11];
fd = open(file, O_RDONLY);
do
{
size = read(fd, buffer, sizeof(buffer));
int j = 0;
for (int i = 0; i < size; ++i)
{
if (buffer[i] < '9' && buffer[i] > '0')
{
num[j++] = buffer[i];
}
else
{
num[j] = '\0';
values[count++] = atoi(num);
j = 0;
}
}
} while (size != 0);
close(fd);
return count;
}
void insert(int* arr, int pos, int temp)
{
while (pos >= 0 && temp < arr[pos])
{
arr[pos + 1] = arr[pos];
--pos;
}
arr[pos + 1] = temp;
}
void serial(int* arr, int size)
{
for (int i = 1; i < size; ++i)
{
insert(arr, i - 1, arr[i]);
}
}
int main(int argc, char *argv[])
{
int* values;
int self, size, length, temp;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &self);
MPI_Status status;
if (self == 0)
{
// 主程序讀檔
values = (int*)malloc(100 * sizeof(int));
length = readValue("/home/gt/parellel/sort/data.in", values);
// 先給每個程序準備一個初始值
serial(values, size);
// 廣播總長度
MPI_Bcast(&length, 1, MPI_INT, ROOT, MPI_COMM_WORLD);
// 把初始區間邊界發給各程序
for (int i = 1; i < size; ++i)
{
MPI_Ssend(&values[i - 1], 1, MPI_INT, i, INIT, MPI_COMM_WORLD);
}
MPI_Recv(&temp, 1, MPI_INT, self + 1, DATA, MPI_COMM_WORLD, &status);
// 逐一廣播剩下的值,各程序自行插入
for (int i = size - 1; i < length; ++i)
{
temp = values[i];
MPI_Bcast(&temp, 1, MPI_INT, ROOT, MPI_COMM_WORLD);
printf("broadcast %d \n", values[i]);
}
// 收尾
printf("ends \n");
temp = END;
MPI_Bcast(&temp, 1, MPI_INT, ROOT, MPI_COMM_WORLD);
// 按序回收
int pos = 0;
int len = 0;
for (int i = 1; i < size; ++i)
{
MPI_Recv(&len, 1, MPI_INT, i, DATA, MPI_COMM_WORLD, &status);
for (int j = 0; j < len; ++j)
{
MPI_Recv(&temp, 1, MPI_INT, i, DATA, MPI_COMM_WORLD, &status);
values[pos++] = temp;
}
}
for (int i = 0; i < length; ++i)
{
printf("%d \n", values[i]);
}
free(values);
}
else
{
MPI_Request request;
MPI_Bcast(&length, 1, MPI_INT, ROOT, MPI_COMM_WORLD);
int pos = 0;
MPI_Irecv(&temp, 1, MPI_INT, ROOT, INIT, MPI_COMM_WORLD, &request);
int* local = (int*) malloc(length * sizeof(int) + 1);
MPI_Wait(&request, &status);
// 與下一個程序交換上界
int upper = 999999; // int 最大值
if (self != size - 1)
MPI_Irecv(&upper, 1, MPI_INT, self + 1, DATA, MPI_COMM_WORLD, &request);
MPI_Ssend(&temp, 1, MPI_INT, self - 1, DATA, MPI_COMM_WORLD);
if (self != size - 1) MPI_Wait(&request, &status);
local[pos++] = temp;
MPI_Bcast(&temp, 1, MPI_INT, ROOT, MPI_COMM_WORLD);
while (temp != END)
{
printf("%d recieve %d \n", self, temp);
if (temp <= upper && (self == 1 || local[0] < temp))
{
printf("%d insert %d \n", self, temp);
local[pos++] = temp;
insert(local, pos - 2, temp);
}
MPI_Bcast(&temp, 1, MPI_INT, ROOT, MPI_COMM_WORLD);
}
// 依序回傳給主程序
MPI_Ssend(&pos, 1, MPI_INT, ROOT, DATA, MPI_COMM_WORLD);
for (int i = 0; i < pos; ++i)
{
MPI_Ssend(&local[i], 1, MPI_INT, ROOT, DATA, MPI_COMM_WORLD);
printf("%d send back %d \n", self, local[i]);
}
free(local);
}
MPI_Finalize();
return 0;
}
關鍵在於區間邊界的交換:每個從程序透過 MPI_Irecv/MPI_Ssend 向相鄰程序取得上界,再配合本地首元素判斷,就能確定某個廣播值是否屬於自己的責任範圍,避免重複插入。