Click here to Skip to main content
Click here to Skip to main content

Base64 Encoding on a GPU

By , 2 Nov 2011
 
Base64EncodingOnGPU_src.zip
Base64EncodingOnGPU
Base64EncodingOnGPU
Properties
CudafyV1.5
bin
Cudafy.NET.dll
CudafyModuleViewer.exe
CUDAfy Documentation.url
CudafyByExample
add_loop_blocks.cdfy
add_loop_gpu.cdfy
add_loop_gpu_alt.cdfy
add_loop_long.cdfy
add_loop_long_blocks.cdfy
basic_double_stream_correct.cdfy
chapter03
chapter04
chapter05
chapter06
chapter09
chapter10
dot.cdfy
hist_gpu_shmem_atomics.cdfy
julia_gpu.cdfy
Properties
ray.cdfy
ray_noconst.cdfy
ripple_gpu.cdfy
simple_kernel.cdfy
simple_kernel_params.cdfy
CudafyExamples
ArrayBasicIndexing.cdfy
ArrayMultidimensions.cdfy
Arrays
Complex
ComplexNumbersD.cdfy
ComplexNumbersF.cdfy
Dummy
DummyComplexFloat.cu
DummyDummyComplexFloatFunction.cu
DummyFunction.cu
DummyFunctions.cdfy
GlobalArrays.cdfy
Misc
PinnedAsyncIO.cdfy
Properties
Serialization
/* 
 * This software is based upon the book CUDA By Example by Sanders and Kandrot
 * and source code provided by NVIDIA Corporation.
 * It is a good idea to read the book while studying the examples!
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cudafy;
using Cudafy.Host;
using Cudafy.Translator;

namespace CudafyByExample
{
    public class add_loop_long
    {
        public const int N = 32 * 1024;

        public static void Execute()
        {
            CudafyModule km = CudafyTranslator.Cudafy();

            GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target);
            gpu.LoadModule(km);

            int[] a = new int[N];
            int[] b = new int[N];
            int[] c = new int[N];

            // allocate the memory on the GPU of same size as specified arrays
            int[] dev_a = gpu.Allocate<int>(a);
            int[] dev_b = gpu.Allocate<int>(b);
            int[] dev_c = gpu.Allocate<int>(c);

            // fill the arrays 'a' and 'b' on the CPU
            for (int i = 0; i < N; i++)
            {
                a[i] = i;
                b[i] = 2 * i;
            }

            // copy the arrays 'a' and 'b' to the GPU
            gpu.CopyToDevice(a, dev_a);
            gpu.CopyToDevice(b, dev_b);

            gpu.Launch(128, 1).add(dev_a, dev_b, dev_c);

            // copy the array 'c' back from the GPU to the CPU
            gpu.CopyFromDevice(dev_c, c);

            // verify that the GPU did the work we requested
            bool success = true;
            for (int i = 0; i < N; i++)
            {
                if ((a[i] + b[i]) != c[i])
                {
                    Console.WriteLine("{0} + {1} != {2}", a[i], b[i], c[i]);
                    success = false;
                    break;
                }
            }
            if (success)
                Console.WriteLine("We did it!");

            // free the memory allocated on the GPU
            gpu.Free(dev_a);
            gpu.Free(dev_b);
            gpu.Free(dev_c);

            // free the memory we allocated on the CPU
            // Not necessary, this is .NET
        }

        [Cudafy]
        public static void add(GThread thread, int[] a, int[] b, int[] c)
        {
            int tid = thread.blockIdx.x;
            while (tid < N)
            {
                c[tid] = a[tid] + b[tid];
                tid += thread.gridDim.x;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of use and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

About the Author

Nick Kopp
Systems Engineer Hybrid DSP Systems
Netherlands Netherlands
Member
Nick is co owner of Hybrid DSP, a company specialized in high speed data acquisition, processing and storage.

CUDAfy.NET took considerable effort to develop and we ask nothing in return from users of the LGPL library other than that you please consider donating to Harmony through Education. This small charity helps handicapped children in developing countries by providing suitable schooling.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 2 Nov 2011
Article Copyright 2011 by Nick Kopp
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid