how to merge matrices and create arrays?

4 views (last 30 days)
Shaun Joseph
Shaun Joseph on 22 Apr 2013
Hey all, so I'm trying to do some things in parallel computing.
The first thing is I'm getting an error with line 19 (K=gpuArray (M) )
So I've messed up something with this
M = linspace(0,4,N);
K = gpuArray(M)
Not sure what. . .
and then I'm trying to create an N x N x 2 array by merging C and C1 (which
are vectors of k and k') so I can use the gpu to speed up the calculations for value iteration
I might not be making any sense...but if at the very least the areas where
I'm not making sense are identified then would be helpful too! It's all about
filling in the blanks at this point.
Thanks !
==========================================================
clear
clc
%This program performs value function iteration for the chapter 5 version
%using the brute-force technique
%First define relevant parameters
beta = 0.99;
A1 = 1;
A2 = 0.5;
y = 1;
P = [0.8,1-0.8;0.3,1-0.3];
toler = 1e-3;
diff = 1;
%An alternative is to use a numerical optimizer
N = 2000;
M = linspace(0,4,N);
K = gpuArray(M); %Grid for capital
V1a = log(K); %This is the initial guess for the value function
V2a = V1a;
% V1a = gpuArray(V1);
% V2a = gpuArray(V2);
colones = ones(N,1);
rowones = ones(1,N);
% g = gpuDevice(1);
%Define consumption in each case
C = A1*y + colones*K - K'*rowones; %Matrix of consumption, for each possible
combination of k and k'
ind = find(C<=0); %Find cases of zero consumption
C(ind) = 1e-6; %Replace zero and negative consumption
U = log(C); %Utility from consumption
U(ind) = -100000; %Where consumption was negative, set utility very low
% keyboard
% Ua = gpuArray(U);
%Now for yl
C1 = A2*y + colones*K - K'*rowones; %Matrix of consumption, for each possible
combination of k and k'
ind1 = find(C1<=0); %Find cases of zero consumption
C1(ind1) = 1e-6; %Replace zero and negative consumption
U1 = log(C1); %Utility from consumption
U1(ind1) = -100000; %Where consumption was negative, set utility very low
% U1a = gpuArray(U1);
clearvars C C1 ind ind1
============================================================================
  1 Comment
Ben Tordoff
Ben Tordoff on 2 May 2013
Edited: Ben Tordoff on 2 May 2013
Hi Shaun,
could you elaborate on what the errors say? What happens if you just type
gpuDevice
at the MATLAB command prompt? This will tell you whether you have a supported GPU and whether MATLAB + Parallel Computing Toolbox can successfully select it. If you post the output and the errors you're seeing someone might be able to help.
Also, could you format the code in your question (indent by two spaces)? That would make it easier to read.
Ben

Sign in to comment.

Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!