Hi,
I am trying to run metal on Xcode 7 beta, OSX 10.11 beta. I am getting this runtime error when I use a buffer to get results out of kernel. Here is the code.
int main(int argc, const char * argv[]) {
NSError *errors;
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
id<MTLLibrary> library = [device newDefaultLibrary];
id<MTLCommandQueue> commandQueue = [device newCommandQueue];
const unsigned length = 100000;
float In1[length], In2[length], Out[length];
for(unsigned i=0;i<length;i++){
In1[i] = 1.0f;
In2[i] = 1.0f;
}
id<MTLCommandBuffer> cb_add = [commandQueue commandBuffer];
id<MTLComputeCommandEncoder> ce_add = [cb_add computeCommandEncoder];
id<MTLFunction> func_add = [library newFunctionWithName:@"add" ];
id<MTLComputePipelineState> pipe_add = [device newComputePipelineStateWithFunction:func_add error:&errors];
[ce_add setComputePipelineState:pipe_add];
MTLSize threadsPerGroup = {100, 1, 1};
MTLSize numThreadgroups = {1, 1, 1};
id<MTLBuffer> In1d = [device newBufferWithBytes:In1 length:length*sizeof(float) options:0];
id<MTLBuffer> In2d = [device newBufferWithBytes:In2 length:length*sizeof(float) options:0];
id<MTLBuffer> Outd = [device newBufferWithBytesNoCopy:Out length:length*sizeof(float) options:0 deallocator:nil];
[ce_add setBuffer:In1d offset:0 atIndex:0];
[ce_add setBuffer:In2d offset:0 atIndex:1];
[ce_add setBuffer:Outd offset:0 atIndex:2];
[ce_add dispatchThreadgroups:numThreadgroups threadsPerThreadgroup:threadsPerGroup];
[ce_add endEncoding];
[cb_add commit];
NSLog(@"%f",Out[8]);
return 0;
}
The kernel is a simple add
kernel void add(const device float *In1 [[buffer(0)]],
const device float *In2 [[buffer(1)]],
device float *Out [[buffer(2)]],
uint tid [[thread_position_in_grid]]){
Out[tid] = In1[tid] + In2[tid];
}