0
\$\begingroup\$

This is a follow-up question for Three-dimensional DCT spatial frequency components illustration and Parallel 3D Discrete Cosine Transformation Implementation in Matlab. I am trying to perform three-dimensional DCT calculation by leveraging GPU computing power. Based on Cris Luengo's answer and the vectorization techniques, a function DCT3D_gpuArray has been implemented.

The experimental implementation

  • DCT3D_gpuArray function implementation:

    function out = DCT3D_gpuArray(x)
        if ~isgpuarray(x)
            x = gpuArray(x);
        end
        out = DCT1D_gpuArray(x, 1);
        out = DCT1D_gpuArray(out, 2);
        out = DCT1D_gpuArray(out, 3);
        [N1,N2,N3] = size(x);
        out = gather(8 .* out ./ (N1 * N2 * N3));
    end
    
    function out = DCT1D_gpuArray(x, dim) 
        N = size(x, dim); 
    
        if (dim == 1 || dim == 2)
            alpha = cos(pi/(2*N)*(2*(0:N-1)'+1)*(0:N-1));
            alpha(:,1) = alpha(:,1) / sqrt(2);
            if dim == 1
                out = pagemtimes(alpha, 'transpose', x, 'none');
            elseif dim == 2
                out = pagetranspose(pagemtimes(alpha, 'transpose', pagetranspose(x), 'none'));
            end
            return;
        end
    
        sz = ones(1,3); 
        sz(dim) = N; 
        index = {':',':',':'}; 
        out = gpuArray(zeros(size(x))); 
    
        for n = 0:N-1
           alpha = cos(pi/(2*N)*(2*(0:N-1)+1)*n);
           alpha = reshape(alpha, sz);
           index{dim} = n + 1;
           sum_result = sum(x .* alpha, dim);
           if n == 0
               sum_result = sum_result / sqrt(2);
           end
           out(index{:}) = sum_result;
        end
    end
    

Test Case for DCT3D_gpuArray function

tic;

%% Create test cells
testCellsSize = 10;

test = zeros(testCellsSize, testCellsSize, testCellsSize);
for x = 1:size(test, 1)
    for y = 1:size(test, 2)
        for z = 1:size(test, 3)
            test(x, y, z) = x * 100 + y * 10 + z;
        end
    end
end

%% Perform test
result = DCT3D_gpuArray(test);

%% Print output
for z = 1:size(result, 3)
    fprintf('3D DCT result: %d plane\n' , z);
    for x = 1:size(result, 1)
        for y = 1:size(result, 2)
            fprintf('%f\t' , result(x, y, z));
        end
        fprintf('\n');
    end
    fprintf('\n');
end

%% Visualize results

for z = 1:size(result, 3)
    figure;
    mesh(result(:, :, z));
end

elapsedTime = toc;

The output of the test code above:

3D DCT result: 1 plane
1726.754760 -80.720722  -0.000000   -8.646042   -0.000000   -2.828427   0.000000    -1.143708   -0.000000   -0.320717   
-807.207224 -0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    
-86.460422  -0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    
-28.284271  -0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    
0.000000    -0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    
-11.437076  -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   
-0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    -0.000000   
-3.207174   -0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    

3D DCT result: 2 plane
-8.072072   -0.000000   -0.000000   0.000000    -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    
-0.000000   0.000000    0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   
0.000000    -0.000000   0.000000    0.000000    -0.000000   0.000000    -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   
-0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   -0.000000   0.000000    0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   -0.000000   
-0.000000   0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   
-0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   
0.000000    -0.000000   0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   
-0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    

3D DCT result: 3 plane
-0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    -0.000000   0.000000    -0.000000   -0.000000   
0.000000    0.000000    0.000000    -0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    
-0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   0.000000    0.000000    
0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   0.000000    
0.000000    -0.000000   0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    
-0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    0.000000    -0.000000   
-0.000000   0.000000    -0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    -0.000000   
0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    
0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    0.000000    -0.000000   
0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   -0.000000   

3D DCT result: 4 plane
-0.864604   0.000000    0.000000    -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   
-0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    
-0.000000   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   -0.000000   
-0.000000   0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   0.000000    
0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   
-0.000000   0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    
0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    -0.000000   -0.000000   
0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    
0.000000    -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   

3D DCT result: 5 plane
-0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    -0.000000   
0.000000    -0.000000   0.000000    0.000000    -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    
-0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    
0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    
0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   0.000000    -0.000000   0.000000    
0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    
0.000000    0.000000    -0.000000   0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    -0.000000   
0.000000    -0.000000   0.000000    0.000000    -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    
0.000000    0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    
-0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   

3D DCT result: 6 plane
-0.282843   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   0.000000    
-0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    
-0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   -0.000000   -0.000000   -0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   -0.000000   
-0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   
-0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   0.000000    0.000000    0.000000    -0.000000   
0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    
-0.000000   -0.000000   -0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   0.000000    0.000000    
0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    
0.000000    0.000000    0.000000    -0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   

3D DCT result: 7 plane
0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   
-0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   
0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   
0.000000    0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   
-0.000000   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   
-0.000000   -0.000000   0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   
0.000000    0.000000    0.000000    0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   
0.000000    -0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    

3D DCT result: 8 plane
-0.114371   -0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   
-0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   0.000000    
-0.000000   0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   
-0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    
0.000000    0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   -0.000000   
-0.000000   0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   
-0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   
-0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   
0.000000    0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   
-0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    

3D DCT result: 9 plane
-0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    0.000000    -0.000000   0.000000    
0.000000    0.000000    0.000000    0.000000    0.000000    -0.000000   0.000000    0.000000    -0.000000   0.000000    
-0.000000   -0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   0.000000    0.000000    -0.000000   
0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    
-0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   
0.000000    -0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    
-0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   0.000000    
-0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   
0.000000    -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    
0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   0.000000    0.000000    0.000000    0.000000    

3D DCT result: 10 plane
-0.032072   -0.000000   0.000000    -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    
-0.000000   -0.000000   -0.000000   0.000000    -0.000000   -0.000000   0.000000    0.000000    -0.000000   0.000000    
0.000000    0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    -0.000000   0.000000    
-0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   
-0.000000   0.000000    0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   0.000000    -0.000000   
-0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   -0.000000   0.000000    
-0.000000   -0.000000   -0.000000   0.000000    0.000000    -0.000000   -0.000000   -0.000000   -0.000000   0.000000    
-0.000000   0.000000    -0.000000   0.000000    0.000000    -0.000000   0.000000    -0.000000   -0.000000   0.000000    
0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    -0.000000   0.000000    -0.000000   -0.000000   
-0.000000   0.000000    0.000000    0.000000    -0.000000   -0.000000   0.000000    0.000000    0.000000    0.000000    

All suggestions are welcome.

The summary information:

\$\endgroup\$
2
  • \$\begingroup\$ Are the dimensions of the transform fixed? E.g code for 8x8x8 dct can run much faster than code for arbitrary dimensions. \$\endgroup\$ Sep 8, 2022 at 7:48
  • \$\begingroup\$ @Björn Lindqvist Thank you for your comment. > Are the dimensions of the transform fixed? If the word "dimensions" means "size" (not rank), nope, you can perform the 3D-DCT calculation using some kind of cube like 5x6x7 with the proposed function, actually. \$\endgroup\$
    – JimmyHu
    Sep 8, 2022 at 12:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.