Click here to Skip to main content
12,395,645 members (64,756 online)
Click here to Skip to main content

Stats

49.2K views
559 downloads
23 bookmarked
Posted

GPU Computing Using CUDA, Eclipse, and Java with JCuda

, 21 Sep 2013 CPOL
Tutorial: GPU computing with JCuda and Nsight (Eclipse)
CodeProjectMBishop_12_22_2012
JCudaFftDemo
bin
CaxpyGpu.class
ComplexCalcFloat.class
ComplexFloat.class
FftCpuFloat.class
FftGpuFloat.class
Main.class
Stopwatch.class
src
Notes
/**
 * Demo Adaption  Mark Bishop; 2012
 * License GNU v3; 
 * This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see http://www.gnu.org/licenses/.
 */

import jcuda.*;
import jcuda.jcublas.JCublas;
import jcuda.runtime.*;

/**
 * 
 * Convenience Class y(n) = Ax(n) + y(n)
 * 
 */
public class CaxpyGpu {

	/**
	 * 
	 * @param scalar
	 *            : a cuComplex number object
	 * @param x
	 *            an interleaved complex number data array
	 * @param y
	 *            an interleaved complex number data array
	 * @return y = ax + y
	 */
	public static float[] CaxpyComplexScalar(cuComplex scalar, float[] x,
			float[] y) {

		int complexElements = x.length / 2;
		int floatElements = x.length;
		int memorySize = floatElements * Sizeof.FLOAT;

		// Allocate memory on the device using JCuda.
		Pointer deviceX = new Pointer();
		Pointer deviceY = new Pointer();
		JCuda.cudaMalloc(deviceX, memorySize);
		JCuda.cudaMalloc(deviceY, memorySize);

		// Copy memory from host to device using JCuda.
		JCuda.cudaMemcpy(deviceX, Pointer.to(x), memorySize,
				cudaMemcpyKind.cudaMemcpyHostToDevice);
		JCuda.cudaMemcpy(deviceY, Pointer.to(y), memorySize,
				cudaMemcpyKind.cudaMemcpyHostToDevice);

		JCublas.cublasInit();
		JCublas.cublasCaxpy(complexElements, scalar, deviceX, 1, deviceY, 1);

		// Copy the result from the device to the host.
		JCuda.cudaMemcpy(Pointer.to(y), deviceY, memorySize,
				cudaMemcpyKind.cudaMemcpyDeviceToHost);

		// Clean up
		JCuda.cudaFree(deviceX);
		JCuda.cudaFree(deviceY);
		JCublas.cublasShutdown();

		return y;
	}

	/**
	 * @param scalar
	 *            : real only as float
	 * @param x
	 *            an interleaved complex number data array
	 * @return y=ax
	 */
	public static float[] CaxpyFloatScalar(float scalar, float[] x) {

		int complexElements = x.length / 2;
		int floatElements = x.length;
		int memorySize = floatElements * Sizeof.FLOAT;
		float y[] = new float[floatElements];

		// Allocate memory on the device using JCuda.
		Pointer deviceX = new Pointer();
		Pointer deviceY = new Pointer();
		JCuda.cudaMalloc(deviceX, memorySize);
		JCuda.cudaMalloc(deviceY, memorySize);

		// Copy memory from host to device using JCuda.
		JCuda.cudaMemcpy(deviceX, Pointer.to(x), memorySize,
				cudaMemcpyKind.cudaMemcpyHostToDevice);
		JCuda.cudaMemcpy(deviceY, Pointer.to(y), memorySize,
				cudaMemcpyKind.cudaMemcpyHostToDevice);

		// Convert scalar to compatible cuComplex number object.
		cuComplex alpha = cuComplex.cuCmplx(scalar, 0.0f);

		// Perform a complex y=a*x+y operation using JCublas .
		JCublas.cublasInit();
		JCublas.cublasCaxpy(complexElements, alpha, deviceX, 1, deviceY, 1);

		// Copy the result from the device to the host.
		JCuda.cudaMemcpy(Pointer.to(y), deviceY, memorySize,
				cudaMemcpyKind.cudaMemcpyDeviceToHost);

		// Clean up
		JCuda.cudaFree(deviceX);
		JCuda.cudaFree(deviceY);
		JCublas.cublasShutdown();

		return y;
	}

	/**
	 * 
	 * @param scalar
	 *            a cuComplex number object
	 * @param x
	 *            an interleaved complex number data array
	 * @return y=ax
	 */
	public static float[] CaxpyComplexScalar(cuComplex scalar, float[] x) {

		int complexElements = x.length / 2;
		int floatElements = x.length;
		int memorySize = floatElements * Sizeof.FLOAT;

		float y[] = new float[floatElements];

		// Allocate memory on the device using JCuda.
		Pointer deviceX = new Pointer();
		Pointer deviceY = new Pointer();
		JCuda.cudaMalloc(deviceX, memorySize);
		JCuda.cudaMalloc(deviceY, memorySize);

		// Copy memory from host to device using JCuda.
		JCuda.cudaMemcpy(deviceX, Pointer.to(x), memorySize,
				cudaMemcpyKind.cudaMemcpyHostToDevice);
		JCuda.cudaMemcpy(deviceY, Pointer.to(y), memorySize,
				cudaMemcpyKind.cudaMemcpyHostToDevice);

		JCublas.cublasInit();
		JCublas.cublasCaxpy(complexElements, scalar, deviceX, 1, deviceY, 1);

		// Copy the result from the device to the host.
		JCuda.cudaMemcpy(Pointer.to(y), deviceY, memorySize,
				cudaMemcpyKind.cudaMemcpyDeviceToHost);

		// Clean up
		JCuda.cudaFree(deviceX);
		JCuda.cudaFree(deviceY);
		JCublas.cublasShutdown();

		return y;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service 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 Code Project Open License (CPOL)

Share

About the Author

Mark H Bishop
Founder PEI Watershed Alliance, Inc.
United States United States
I am an analytical chemist and an educator. I program primarily to perform matrix computations for regression analysis, process signals, acquire data from sensors, and to control devices.

I participate in many open source development communities and Linux user forums. I occasionally perform IT contract work, primarily focused on network design/deployment and penetration testing for small organizations.

I am a member of several community-interest groups such as the Prince Edward Island Watershed Alliance, the Lot 11 and Area Watershed Management Group, and the Petersham Historic Commission.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 21 Sep 2013
Article Copyright 2012 by Mark H Bishop
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid