解決使用GPU處理影象時提示資料不能是gpuArray的問題
【時間】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