---restore content starts---
Let's start by introducing a few of the functions we just learned today:
1, Linspace. Produces a specified number of points in the specified range, adjacent data spans the same, and returns a row vector. Its invocation form in the CPU and GPU
X=linspace (5,100,20) % produces 20 data in the range from 5 to 100, the adjacent data span is the same x=gpuarray.linspace (5,100,20) % produces 100 data from 5 to 20, Contiguous data spans are the same
Output:
X = 5 ( 70) in ten 100gX = 5 ( 35). 100-in-a-
2, Arrayfun. Invocation Form Example: idx = Arrayfun (@calcidx, Z0); % calls the CALCIDX function for each element in Z0 and returns the result to the IDX. (PS: Avoid unnecessary loops)
S=[1 2;3 4];s=gpuarray (s); % in the CPU can not this sentence [email protected] (x) x^2;arrayfun (f,s) ans = 1 4 9 16
Note: When writing a sub-function, especially in the GPU operation, the following problems may occur, although I have not met, and do not know why.
function output = CALCIDX (input) % Calc index value for per element in Z0 z = complex (0,0); k = 0; % This initialization k cannot be saved, otherwise error for k = 0:max_iter-1 z = z * z + input; If ABS (z) > 2, break; End End output = k;
3. About function calls in the GPU
As with the traditional Matlab function call method, you can move the variable to the GPU before calling it with the Gpuarray () function, or relocate the variable to the GPU in the child function. Of course I recommend the first kind.
The specific invocation comparison is as follows:
[Tmprmse_me] = Rmse_me (DATATRAINPCA ', Labeltrain ', DATATESTPCA ', Labeltest ', param); % CPU [TMPRMSE_ME1] = Rmse_me (Gpuarray (DATATRAINPCA '), Gpuarray (Labeltrain '), Gpuarray (DATATESTPCA '), GpuArray ( Labeltest '), param); In the% GPU
Note: GPU arrays support is only fundamental numeric or logical data types. (GPU)
Errors that are prone to occur are as follows:
An unexpected error occurred during CUDA execution. The CUDA error Was:cuda
Workaround: Restart MATLAB or restart your computer because the GPU is crashing.
---restore content ends---
Matlab+gpu Accelerated Learning Notes (ii)