Info

This question is closed. Reopen it to edit or answer.

How can I store dates as arrays for further process when using Matlab GPU computing?

2 views (last 30 days)
In the following attached code, I would like to record the variable Z in the while loop as array for further process, for example, finding the max value of Z in the array. But the dynamic array is not supported in the arrayfun for GPU computing. How to solve this problem?
function test_GPU_computing
maxIterations=500;
gridSize=1500;
xlim=[-0.75, -0.73];
ylim=[ 0.12, 0.14];
t=tic();
x=gpuArray.linspace( xlim(1), xlim(2), gridSize );
y=gpuArray.linspace( ylim(1), ylim(2), gridSize );
[xGrid,yGrid] = meshgrid( x, y );
count=arrayfun( @tar_fun,xGrid, yGrid, maxIterations );
count=gather(count);
gpuArrayfunTime=toc(t)
figure(3)
imagesc(x,y,count)
reset(gpuDevice(1))
function count=tar_fun(x0,y0,maxIterations)
z0=complex(x0,y0);
z=z0;
count=1;
while (count <= maxIterations) && (abs(z) <= 2)
count=count+1;
z=z*z+z0;
end
count=log(count);

Answers (2)

Walter Roberson
Walter Roberson on 15 Dec 2018
It does not need to be dynamic. You have maxIterations, so you can create it that size. When you need the "dynamic" version of it, index to the number actually used.
  3 Comments
Shan Yin
Shan Yin on 17 Dec 2018
Thank you, Walter Roberson.
I have changed my code to try the nested function, but the situation did not improve. Though the array can be indexed if it is not assigned, it does not reach to my purpose.
So, can you tell me whether I can achieve my goal by using the arrayfun funtion?
clear all
clc
maxIterations=100;
gridSize=300;
xlim=[-0.75, -0.73];
ylim=[ 0.12, 0.14];
t=tic();
x=gpuArray.linspace(xlim(1), xlim(2), gridSize);
y=gpuArray.linspace(ylim(1), ylim(2), gridSize);
[xGrid,yGrid]=meshgrid(x,y);
Pos=gpuArray.zeros(maxIterations,1);
count=parent_fun(xGrid,yGrid,maxIterations,Pos);
count=gather(count);
gpuArrayfunTime=toc(t)
figure(1)
imagesc(x,y,count)
reset(gpuDevice(1))
function result=parent_fun(xGrid,yGrid,maxIterations,Pos)
function count=tar_fun(x0,y0)
z0=complex(x0,y0);
z=z0;
count=1;
while (count<=maxIterations) && (abs(z)<=2)
count=count+1;
z=z*z+z0;
% z=z*z+z0+Pos(count,1); % This line can be executed, but the following line can not!
Pos(count,1)=z; % where error arises!
end
count=max(log(count),log(abs(z)));
end
result=arrayfun(@tar_fun, xGrid, yGrid);
end

Edric Ellis
Edric Ellis on 17 Dec 2018
If you know the reduction operation you wish to perform on z, then you might be able to update a "running total" as the while loop proceeds, like so:
function [count,maxZ]=tar_fun(x0,y0,maxIterations)
z0=complex(x0,y0);
z=z0;
count=1;
% Initialize maxZ
maxZ = complex(0);
while (count <= maxIterations) && (abs(z) <= 2)
z=z*z+z0;
% Update the value of maxZ
if count == 1 || abs(z) > abs(maxZ)
maxZ = z;
end
count=count+1;
end
count=log(count);
  1 Comment
Shan Yin
Shan Yin on 17 Dec 2018
Thanks for your kind reply and the code you provide.
In my question, finding the max value of variable z was taken as example to explain my purpose of using an array. However, it is not all of my goal. Indeed, I plan to transfer this simple case to some more realistic and complicated cases, e.g., dynamic systems described by ordinary differential equation. In that situation, an array would be required to record the evolution of every system state variable, to further determine the long-term behavior of the dynamic systems.
So, in this posted question, I do need to know directions for using an array in the arrayfun function, rather than finding the max value of variable z simplely. Hope I have explained my purpose clearly.
Thanks again for your comment. And please tell me if you have more ideas on how to use array in the arrayfun function.
Shan Yin

Tags

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!