【时间】2018.10.11
【题目】解决使用GPU处理图像时提示数据不能是gpuArray的问题
【问题描述】今天我在MATLAB中,用GPU进行图像处理,最后想用imwrite保存下来,但出现了以下错误:错误使用 imwrite 需要的 DATA 应为以下类型之一:numeric, logical ,但其类型是 gpuArray。出错 test (line 1) imwrite(X,'01.png');
【出错原因】没有将GPU上的数据回传给CPU
【解决办法】在imwrite语句之前,使用gather函数,即X= gather(X),这个语句的作用是将GPU上的数据回传给CPU,详细用法如下:
【 gather函数用法】
>> help gather
gather collect values into current workspace
X = gather(A) when A is a codistributed array, X is a replicated array with
all the data of the array on every lab. This would typically be executed
inside SPMD statements, or in parallel jobs.
X = gather(A) when A is a distributed array, X is an array in the local
workspace with the data transferred from the multiple labs. This would
typically be executed outside SPMD statements.
X = gather(A) when A is a gpuArray, X is an array in the local workspace
with the data transferred from the GPU device.
If A is not one of the types mentioned above, then no operation is
performed and X is the same as A.
Example:
% create a distributed array
d = distributed(magic(5));
% gather values back to the client
x = gather(d);
% a second gather is a no-op
isequal(x, gather(x)) % returns true