ソース 5.3.3 単純な汎用データ型 (C++)
| maindefs.h |
|---|
#ifndef ___MAINDEFS_H__200211211819_JEWFJODSJF__INCLUDED___ #define ___MAINDEFS_H__200211211819_JEWFJODSJF__INCLUDED___ #include <mpi++.h> /* 配列の要素数を返します */ #define NUMOF(array) (sizeof (array) / sizeof *(array)) /* 配列型の要素数を返します */ #define NUMOFT(type) NUMOF(*(type*)0) /* 配列型の文字列の長さを返します */ #define STRLEN(literal) (NUMOF(literal) - 1) #endif /* #ifndef ___MAINDEFS_H__200211211819_JEWFJODSJF__INCLUDED___ */ |
| newtype.h |
#ifndef ___NEWTYPE_H__200211211820_DAOFIJE3W__INCLUDED___ #define ___NEWTYPE_H__200211211820_DAOFIJE3W__INCLUDED___ #include "maindefs.h" /* int 2 個からなる新しい型です */ typedef int NEWTYPE_T[2]; /* 新しい MPI の汎用データ型を作成、登録します */ void CreateNewType(MPI::Datatype& newType); /* MPI の新しいデータ型を開放します */ void FreeNewType(MPI::Datatype& newType); /* 新しい型の配列を乱数で初期化します */ void InitNewType(NEWTYPE_T pNewType[], int size); /* 新しい型の配列を表示します */ void OutputNewType(NEWTYPE_T pNewType[], int size); #endif /* #ifndef ___NEWTYPE_H__200211211820_DAOFIJE3W__INCLUDED___ */ |
| newtype.cpp |
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "maindefs.h"
#include "newtype.h"
using namespace std;
/* 新しい MPI の汎用データ型を作成、登録します */
void CreateNewType(MPI::Datatype& newType)
{
/* int 型 2 個からなる新しい汎用データ型を作成します */
newType = MPI::INT.Create_contiguous(NUMOFT(NEWTYPE_T));
/* その汎用データ型を MPI が使用できるように登録します */
newType.Commit();
}
/* MPI の新しいデータ型を開放します */
void FreeNewType(MPI::Datatype& newType)
{
newType.Free();
}
/* 新しい型の配列を乱数で初期化します */
void InitNewType(NEWTYPE_T pNewType[], int size)
{
for(int i = 0; i < size; ++i)
{ for(int j = 0; j < NUMOF(pNewType[0]); ++j)
{ pNewType[i][j] = rand(); }
}
}
/* 新しい型の配列を表示します */
void OutputNewType(NEWTYPE_T pNewType[], int size)
{
for(int i = 0; i < size; ++i)
{
cout << " " << i << ": <";
for(int j = 0; j < NUMOF(pNewType[0]); ++j)
{
if(j != 0)
{ cout << ", "; }
cout << setw(11) << pNewType[i][j];
}
cout << '>' << endl;
}
} |
| main.cpp |
#include <iostream>
#include "maindefs.h"
#include "newtype.h"
#define NUMOF(array) (sizeof (array) / sizeof *(array))
#define STRLEN(literal) (NUMOF(literal) - 1)
enum TAG_KIND // 通信タグ
{
TAG_NEWTYPE, // 新しいデータ型のテスト用通信タグ
};
enum RANK_KIND // ランク
{
RANK_SENDER, // 送信側のランク
RANK_RECVER, // 受信側のランク
};
using namespace std;
// 送信
void Send(MPI::Datatype newType)
{
/* 送信するデータを作成し、出力します */
NEWTYPE_T data[9];
InitNewType(data, NUMOF(data));
cout << "Sended data :" << endl;
OutputNewType(data, NUMOF(data));
/* データ型に newType を指定します */
MPI::COMM_WORLD.Send(data, NUMOF(data), newType,
RANK_RECVER, TAG_NEWTYPE);
}
// 受信
void Recv(MPI::Datatype newType)
{
NEWTYPE_T data[64];
int recvCount;
MPI::Status status;
/* データ型に newType を指定します */
MPI::COMM_WORLD.Recv(data, NUMOF(data), newType,
RANK_SENDER, TAG_NEWTYPE, status);
recvCount = status.Get_count(newType);
cout << "Received count : " << recvCount << endl;
cout << "Received data :" << endl;
OutputNewType(data, recvCount);
}
int main(int argc, char** argv)
{
MPI::Init(argc, argv);
int rank = MPI::COMM_WORLD.Get_rank();
MPI::Datatype newType; /* 新しい MPI のデータ型 */
CreateNewType(newType); /* 新しいデータ型を作成、登録します */
switch(rank)
{
case RANK_SENDER: Send(newType); break;
case RANK_RECVER: Recv(newType); break;
}
FreeNewType(newType); /* 新しいデータ型の登録を解除します */
MPI::Finalize();
cout << "Exit : " << rank << endl;
} |
Last update was done on 2002.11.14