2016-05-17 14 views
0

次のような簡単なコードを書いて、GPUで計算ができるかどうかを確認しました。は、金属を使用しているときにgpuからデータを取得できませんでした。

id<MTLDevice> device = MTLCreateSystemDefaultDevice(); 
NSLog(@"Device: %@", [device name]); 

id<MTLCommandQueue> commandQueue = [device newCommandQueue]; 

NSError * ns_error = nil; 
id<MTLLibrary>defaultLibrary = [device newLibraryWithFile:@"/Users/i/tmp/tmp6/s.metallib" error:&ns_error]; 

// Buffer for storing encoded commands that are sent to GPU 
id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer]; 

// Encoder for GPU commands 
id <MTLComputeCommandEncoder> computeCommandEncoder = [commandBuffer computeCommandEncoder]; 

//set input and output data 
float tmpbuf[1000]; 
float outbuf[1000]; 
for(int i = 0; i < 1000; i++) 
{ 
    tmpbuf[i] = i; 
    outbuf[i] = 0; 
} 

int tmp_length = 100*sizeof(float); 
id<MTLBuffer> inVectorBuffer = [device newBufferWithBytes: tmpbuf length: tmp_length options: MTLResourceOptionCPUCacheModeDefault ]; 
[computeCommandEncoder setBuffer: inVectorBuffer offset: 0 atIndex: 0 ]; 
id<MTLBuffer> outVectorBuffer = [device newBufferWithBytes: outbuf length: tmp_length options: MTLResourceOptionCPUCacheModeDefault ]; 
[computeCommandEncoder setBuffer: outVectorBuffer offset: 0 atIndex: 1 ]; 


//get fuction 
id<MTLFunction> newfunc = [ defaultLibrary newFunctionWithName:@"sigmoid" ]; 

//get pipelinestat 
id<MTLComputePipelineState> cpipeline = [device newComputePipelineStateWithFunction: newfunc error:&ns_error ]; 

[computeCommandEncoder setComputePipelineState:cpipeline ]; 

// 
MTLSize ts= {10, 10, 1}; 
MTLSize numThreadgroups = {2, 5, 1}; 
[computeCommandEncoder dispatchThreadgroups:numThreadgroups threadsPerThreadgroup:ts]; 
[ computeCommandEncoder endEncoding ]; 
[ commandBuffer commit]; 

//get data computed by GPU 
NSData* outdata = [NSData dataWithBytesNoCopy:[outVectorBuffer contents ] length: tmp_length freeWhenDone:false ]; 
float final_out[1000]; 
[outdata getBytes:final_out length:tmp_length]; 

//In my option, each value of final_out should be 0 
for(int i = 0; i < 1000; i++) 
{ 
    printf("%.2f : %.2f\n", tmpbuf[i], final_out[i]); 
} 

シェーダファイル名s.shaderは、上記のコードでは値10.0

using namespace metal; 
kernel void sigmoid(const device float *inVector [[ buffer(0) ]], 
       device float *outVector [[ buffer(1) ]], 
       uint id [[ thread_position_in_grid ]]) { 
    // This calculates sigmoid for _one_ position (=id) in a vector per call on the GPU 
    outVector[id] = 10.0; 
} 

と出力を割り当てており、以下の通りである、私は可変final_outによりGPUによって計算されたデータを得ました。私の選択では、s.shaderに示されているように、final_outの各値は10.0でなければなりません。しかし、final_outの値はすべて0です。GPUからデータを取り戻す際の問題はありますか?おかげさまで

答えて

2

コマンドバッファをコミットするだけで、ドライバに実行を開始するように指示するだけです。 CPU上のGPU操作の結果を読み戻す場合は、-waitUntilCompletedで現在のスレッドをブロックするか、コマンドバッファが-addCompletedHandler:メソッドで完了したときに呼び出されるブロックを追加する必要があります。

その他のメモ:ストレージモードがSharedのバッファを使用しているようです。ストレージモードがManagedのバッファを使用する場合は、blitコマンドエンコーダを作成し、適切なバッファを使用してsynchronizeResource:を呼び出して、前述のように完了するまで待ってから、コピーする必要がありますGPUからの結果を元に戻します。

+0

ありがとうございますwarrenm、私はあなたが言ったようにコードを改善するでしょう。 – Pony

+0

問題はまだあります。私は「[commandBuffer waitUntilCompleted];」を追加しました。 commandBufferがコミットした後、ストレージモードがManagedのバッファは使用しませんでした。しかし、GPUからのデータの取得に失敗しました。 – Pony

+0

2Dグリッドをディスパッチしていますが、 'thread_position_in_grid'パラメータは1Dです。これは無効な設定で、検証レイヤーを有効にして実行している場合にアサーションを実行する必要があります。 – warrenm

関連する問題