MPI-2 を使用して最適化プログラムを作成しています。このプログラムでは、(概念的に)std::vector
等しい長さstd::vector
の s をすべてのプロセスで共有する必要があります。このベクトルは、k
現在見つかっている問題の最適解を保持し、多くの MPI プロセスの 1 つによって新しい最適解が見つかるたびに更新されます。通常、新しいソリューションを見つけるために各プロセスが費やす時間は大きく異なります。
MPI_allgather
私の質問は、同期と待機のパフォーマンスの問題を考慮して、新しい最適なソリューションが見つかるたびにMPI 集合を使用する必要があるかどうかです。または、MPI-2 で One-Sided-Communications を使用して、すべてのプロセス間で「共有」ベクトルを維持する必要があります。
特に、私が を使用する場合MPI_allgather
、プロセスは早期にジョブを終了してアイドル状態になり、他のプロセスとの何らかの同期を待ちますか?
MPI ポイントツーポイント通信 (upd: と UPC) の実務経験はありますが、実際のコーディングではコレクティブや片側通信を使用したことはありません。私は SO を検索し、 MPI_allgathersに関する関連する質問/回答を見つけました。しかし、2 つのアプローチの正確な違いを伝えるのに苦労しています。
ありがとう、
- - アップデート - -
特に、「 MPI プロセス全体で同期を維持するカウンターの作成」のコード例を最後に示します。これは、片側を使用して単一のint
「共有」を維持します。data
ジェネリック型で機能するように適応させようとしましたが、元のコードと配列を維持する理由、およびユーザー関数に一般化する方法を理解するのに苦労しているため、機能させる方法がわかりませんMPI_Accumulate
(単に古いベクトルと新しいベクトル)。
テンプレート //注: T は、int や double などのプリミティブ型 (ポインター、ref、または構造体ではない) のみにすることができます。struct mpi_array { typedef std::vector ベクトル; MPI_Win 勝つ; int ホストランク;
int ランク;
int サイズ;
ベクトル値;
ベクトル *hostvals; };
片側通信カウンター コード:
#include <mpi.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
struct mpi_counter_t {
MPI_Win win;
int hostrank ;
int myval;
int *data;
int rank, size;
};
struct mpi_counter_t *create_counter(int hostrank) {
struct mpi_counter_t *count;
count = (struct mpi_counter_t *)malloc(sizeof(struct mpi_counter_t));
count->hostrank = hostrank;
MPI_Comm_rank(MPI_COMM_WORLD, &(count->rank));
MPI_Comm_size(MPI_COMM_WORLD, &(count->size));
if (count->rank == hostrank) {
MPI_Alloc_mem(count->size * sizeof(int), MPI_INFO_NULL, &(count->data));
for (int i=0; i<count->size; i++) count->data[i] = 0;
MPI_Win_create(count->data, count->size * sizeof(int), sizeof(int),
MPI_INFO_NULL, MPI_COMM_WORLD, &(count->win));
} else {
count->data = NULL;
MPI_Win_create(count->data, 0, 1,
MPI_INFO_NULL, MPI_COMM_WORLD, &(count->win));
}
count -> myval = 0;
return count;
}
int increment_counter(struct mpi_counter_t *count, int increment) {
int *vals = (int *)malloc( count->size * sizeof(int) );
int val;
MPI_Win_lock(MPI_LOCK_EXCLUSIVE, count->hostrank, 0, count->win);
for (int i=0; i<count->size; i++) {
if (i == count->rank) {
MPI_Accumulate(&increment, 1, MPI_INT, 0, i, 1, MPI_INT, MPI_SUM,
count->win);
} else {
MPI_Get(&vals[i], 1, MPI_INT, 0, i, 1, MPI_INT, count->win);
}
}
MPI_Win_unlock(0, count->win);
count->myval += increment;
vals[count->rank] = count->myval;
val = 0;
for (int i=0; i<count->size; i++)
val += vals[i];
free(vals);
return val;
}
void delete_counter(struct mpi_counter_t **count) {
if ((*count)->rank == (*count)->hostrank) {
MPI_Free_mem((*count)->data);
}
MPI_Win_free(&((*count)->win));
free((*count));
*count = NULL;
return;
}
void print_counter(struct mpi_counter_t *count) {
if (count->rank == count->hostrank) {
for (int i=0; i<count->size; i++) {
printf("%2d ", count->data[i]);
}
puts("");
}
}
int test1() {
struct mpi_counter_t *c;
int rank;
int result;
c = create_counter(0);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
result = increment_counter(c, 1);
printf("%d got counter %d\n", rank, result);
MPI_Barrier(MPI_COMM_WORLD);
print_counter(c);
delete_counter(&c);
}
int test2() {
const int WORKITEMS=50;
struct mpi_counter_t *c;
int rank;
int result = 0;
c = create_counter(0);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
srandom(rank);
while (result < WORKITEMS) {
result = increment_counter(c, 1);
if (result <= WORKITEMS) {
printf("%d working on item %d...\n", rank, result);
sleep(random() % 10);
} else {
printf("%d done\n", rank);
}
}
MPI_Barrier(MPI_COMM_WORLD);
print_counter(c);
delete_counter(&c);
}
int main(int argc, char **argv) {
MPI_Init(&argc, &argv);
test1();
test2();
MPI_Finalize();
}