私は、主に行列とベクトルに関連する重い数学計算を行う GPU アルゴリズムに取り組んでいます。処理時間に関してはかなりの数値を記録していますが、まだ改善の余地があると感じています。
だから私は自分でフレームワークを発見しましMetal Performance Shadersた。GPU アルゴリズムが行う数学演算用に微調整および最適化されたカーネル シェーダーを見つけることができるため、そのフレームワークの説明は私を興奮させました。
ベクトルの出力で行列をMPSMatrixVectorMultiplication大きく乗算しているため、最初に使用することにしました。11000x50011000 vector500
そういうわけで、私はそれを使用しています。s と操作自体のMPSラッパーを宣言します。MTLBuffer
MPSMatrix *model;
MPSVector *vector;
id<MTLBuffer> resultBuffer;
MPSVector *resultVector;
MPSMatrixVectorMultiplication *matrixVectorMultiplication;
これらのMPSラッパーを初期化します。
matrixVectorMultiplication = [[MPSMatrixVectorMultiplication alloc] initWithDevice:_ctx.device transpose:true rows:500 columns:11000 alpha:1 beta:0];
//......//
MPSVectorDescriptor *desc = [MPSVectorDescriptor vectorDescriptorWithLength:11000 dataType:MPSDataTypeFloat32];
vector = [[MPSVector alloc] initWithBuffer:vecBuffer descriptor:desc];
MPSVectorDescriptor *desc_out = [MPSVectorDescriptor vectorDescriptorWithLength:500 dataType:MPSDataTypeFloat32];
resultVector = [[MPSVector alloc] initWithBuffer:resultBuffer descriptor:desc_out];
//......//
MPSMatrixDescriptor *desc = [MPSMatrixDescriptor matrixDescriptorWithRows:11000 columns:500 rowBytes:500 * sizeof(float) dataType:MPSDataTypeFloat32]; //I need to transpose the matrix
model = [[MPSMatrix alloc] initWithBuffer:testBuffer descriptor:desc];
そして乗算を行います:
id<MTLCommandBuffer> cmdBuffer = [_ctx.commandQueue commandBuffer];
id<MTLComputeCommandEncoder> encoder = [cmdBuffer computeCommandEncoder];
// work with my own encoder, execute some commands
[encoder endEncoding];
[matrixVectorMultiplication encodeToCommandBuffer:cmdBuffer inputMatrix:model inputVector:vector resultVector:resultVector];
[cmdBuffer commit];
[cmdBuffer waitUntilCompleted]; // I have to wait because my algorithm is sequential at this point
今、私が書いたカーネル関数は、約0.8-1.1ミリ秒でまったく同じ乗算を行います。そして、MPSMatrixVectorMultiplicationそれが18-19 ms.
それはあまりにも遅すぎて、私はそのような結果を信じることができません. 明らかに、パフォーマンスの多くを奪う小さなディテールが欠けています。
MPSパフォーマンスに敏感なコードでソリューションを使用した人はいますか? GPU ルーチンに適用できるヒントをいくつか教えていただければ幸いです。
前もって感謝します!