インテル® C++ コンパイラー 16.0 ユーザー・リファレンス・ガイド
次に、インテルの C++ 非同期 I/O テンプレート・クラスの使用例を示します。float 型の配列を外部ファイルに書き込む次のコードについて考えてみます。
// Data is array of floats std::vector<float> v(10000); // User defines new operator << for std::vector<float> type std::ofstream& operator << (std::ofstream & str, std::vector<float> & vec) { // User’s output actions ... } ... // Output file declaration – object of standard ofstream STL class std::ofstream external_file(“output.txt”); ... // Output operations external_file << v;
次のように上記のコードを変更して、出力処理を非同期に実行します。
// Add new header to support STL asynchronous IO operations #include <aiostream.h> ... std::vector<float> v(10000); std::ofstream& operator << (std::ofstream & str, std::vector<float> & vec) {... } ... // Declare output file as the instance of new async::async_class template // class. // New inherited from STL ofstream type is declared async::async_class<std::ofstream> external_file(“output.txt”); ... external_file << v; ... // Add stop operation, to wait the completion of all asynchronous IO //operations external_file.wait(); …
パフォーマンスに関する推奨事項
小さなオブジェクトでは非同期モードの使用を推奨しません。例えば、一般的な型の値を出力するループで、STL ストリームへの出力にかかる時間よりも、ほかのループ処理の実行にかかる時間のほうが短い場合、非同期モードを使用しないでください。
ただし、小さなデータの出力とループ内におけるその計算のバランスが取れる場合は、一定のパフォーマンス向上が得られます。
例えば、次のコードは外部ファイルから 2 つの行列を読み取り、ループ内で別の行列の要素を求めて、その結果を出力します。
#define ARR_LEN 900 { std::ifstream fA("A.txt"); fA >> A; std::ifstream fB("B.txt"); fB >> B; std::ofstream fC(f); for(int i=0; i< ARR_LEN; i++) { for(int j=0; j< ARR_LEN; j++) { C[i][j] = 0; for(int k=0; k < ARR_LEN; k++) C[i][j]+ = A[i][k]*B[k][j]*sin((float)(k))*cos((float)(-k))*sin((float)(k+1) )*cos((float)(-k-1)); fC << C[i][j] << std::endl; } } }
行列のサイズが大きい場合、2 つのファイルからのデータの読み取りを並列に実行することで、さらにパフォーマンスを向上できます。