Vectorised a nested conditional function for gpuarray & arrayfun
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Sendy Phang
il 9 Ott 2014
Risposto: Daniel Simmons
il 10 Ott 2014
Hi, I have a class which has many arrays and a nested functions.
The goal is I want to make all the arrays as gpuarray and use arrayfun, to be run in GPU, thus I'd like to have them well vectorised. Any thought? Thanks
%%the class
classdef egclass < handle
properties
cond = [1 1 1 0 0];
x = [10 20 30 40 50];
y = [0 0 0 0 0];
out= [0 0 0 0 0];
end
end
%%the function
function doOperation(eg)
[nR,nC] = size(eg.cond);
%%need to be vectorised!!!
for i = 1:nC
if eg.cond(i) == 0
doTimes(eg,i);
end
if eg.cond(i) == 1
doAdd(eg,i);
end
end
end
function doTimes(eg,i)
eg.out(i) = eg.x(i) * eg.y(i);
end
function doAdd(eg,i)
eg.out(i) = eg.x(i) + eg.y(i);
end
%%debug.m
clear all; clc;
eg = egclass;
doOperation(eg);
0 Commenti
Risposta accettata
Daniel Simmons
il 10 Ott 2014
Hi Sendy,
If you're trying to use arrayfun to pass the arrays to the GPU inside the class, this is no problem! Just use this as your member function:
function doOperation(eg)
gpu_x = gpuArray( eg.x );
gpu_y = gpuArray( eg.y );
gpu_cond = gpuArray( eg.cond );
eg.out = arrayfun( @doAddOrTimes, gpu_cond , gpu_x, gpu_x );
end
where the "doAddOrTimes" function will incorporate the "if" statement. Of course, this function, and any others used on the GPU, will have to be an external function with no class attributes. As far as I'm aware, manipulating classes on the GPU is not possible.
Dan
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su GPU Computing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!