Click here to Skip to main content
11,415,046 members (76,610 online)
Click here to Skip to main content
Add your own
alternative version

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.zip
CodeProjectMBishop_12_22_2012
JCudaFftDemo
bin
CaxpyGpu.class
ComplexCalcFloat.class
ComplexFloat.class
FftCpuFloat.class
FftGpuFloat.class
Main.class
Stopwatch.class
src
Notes
/**
 * Author Mark Bishop; 2003
 * 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/.
 */

/**
 * A utility class for complex number calculations on the CPU using the
 * ComplexFloat type. The type "ComplexFloat" and arrays of type ComplexFloat[]
 * are not interleaved data. Do not confuse this with the (GPU) cuComplex type
 * or CUDA interleaved data arrays.
 */
public class ComplexCalcFloat {

	public static ComplexFloat Add(ComplexFloat arg1, ComplexFloat arg2) {
		ComplexFloat result = new ComplexFloat();
		result.Real = arg1.Real + arg2.Real;
		result.Imaginary = arg1.Imaginary + arg2.Imaginary;
		return result;
	}

	/**
	 * 
	 * @return arg1 - arg2
	 */
	public static ComplexFloat Subtract(ComplexFloat arg1, ComplexFloat arg2) {
		ComplexFloat result = new ComplexFloat();
		result.Real = arg1.Real - arg2.Real;
		result.Imaginary = arg1.Imaginary - arg2.Imaginary;
		return result;
	}

	public static ComplexFloat Multiply(ComplexFloat arg1, ComplexFloat arg2) {
		ComplexFloat result = new ComplexFloat();
		result.Real = (arg1.Real * arg2.Real)
				- (arg1.Imaginary * arg2.Imaginary);
		result.Imaginary = (arg1.Real * arg2.Imaginary)
				+ (arg1.Imaginary * arg2.Real);
		return result;
	}

	public static ComplexFloat Multiply(ComplexFloat arg1, float arg2) {
		ComplexFloat result = new ComplexFloat();
		result.Real = arg1.Real * arg2;
		result.Imaginary = arg1.Imaginary * arg2;
		return result;
	}

	public static ComplexFloat Conjugate(ComplexFloat Z) {
		ComplexFloat result = new ComplexFloat();
		result.Real = Z.Real;
		result.Imaginary = -Z.Imaginary;
		return result;
	}

	// Vector management utilities

	public static ComplexFloat[] InitializeVector(ComplexFloat[] vector) {
		for (int i = 0; i < vector.length; i++) {
			vector[i] = new ComplexFloat();
		}
		return vector;
	}

	public static ComplexFloat FloatToComplex(float real) {
		ComplexFloat result = new ComplexFloat();
		result.Real = real;
		return result;
	}

	public static ComplexFloat[] FloatToComplex(float[] real) {
		int len = real.length;
		ComplexFloat[] result = new ComplexFloat[len];
		for (int i = 0; i < len; i++) {
			result[i] = new ComplexFloat();
			result[i].Real = real[i];
		}
		return result;
	}

	public static ComplexFloat[] InterleavedToComplex(float[] InterleavedArray) {
		int len = InterleavedArray.length;
		ComplexFloat[] result = new ComplexFloat[len / 2];
		for (int i = 0; i < len; i += 2) {
			result[i / 2] = new ComplexFloat();
			result[i / 2].Real = InterleavedArray[i];
			result[i / 2].Imaginary = InterleavedArray[i + 1];
		}
		return result;
	}

	public static float[] ComplexToInterleaved(ComplexFloat[] cVector) {
		int vN = cVector.length;
		int retLen = vN * 2;
		float result[] = new float[retLen];
		for (int i = 0; i < vN; i++) {
			result[2 * i] = cVector[i].Real;
			result[2 * i + 1] = cVector[i].Imaginary;
		}
		return result;
	}

}

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
President PEI Watershed Alliance, Inc.
United States United States
I am a scientist and not a professional programmer. I program primarily to perform matrix computations for regression analysis, process signals, acquire data from sensors, and to control devices.

I have a personal web page at www.mark-bishop.net

I am the Chairperson and one of the primary founders of the Prince Edward Island Watershed Alliance, Inc.: http://peiwatershedalliance.org/web/

I participate in many open source development communities and Linux user forums.
Follow on   LinkedIn

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