Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / CUDA

GPU Computing Using CUDA, Eclipse, and Java with JCuda

Rate me:
Please Sign up or sign in to vote.
4.71/5 (8 votes)
21 Sep 2013CPOL18 min read 102.8K   805   25  
Tutorial: GPU computing with JCuda and Nsight (Eclipse)
/**
 * Adaption of JCuda.org example.
 * Author 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.jcufft.JCufft;
import jcuda.jcufft.cufftHandle;
import jcuda.jcufft.cufftType;

/**
 * Computation class for FFT on Central Processor Unit. and related utilities.
 */

public class FftGpuFloat {

	/**
	 * 
	 * @param input
	 *            an interleaved array (ft)
	 * @return C2C FFT (hf)
	 */
	public static float[] C2C_1D(float[] input) {

		float outputJCufft[] = input.clone();

		// The plan uses the number of complex elements in the input array, i.e:
		// length/2.
		cufftHandle plan = new cufftHandle();
		JCufft.cufftPlan1d(plan, input.length / 2, cufftType.CUFFT_C2C, 1);
		JCufft.cufftExecC2C(plan, outputJCufft, outputJCufft,
				JCufft.CUFFT_FORWARD);
		JCufft.cufftDestroy(plan);

		return outputJCufft;
	}

	/**
	 * 
	 * @param input
	 *            an interleaved array (hf)
	 * @param isNormalize
	 *            Set true if result should be multiplied by 1/N
	 * @return C2C IFFT (ft)
	 */
	public static float[] InverseC2C_1D(float[] input, boolean isNormalize) {

		float outputJCufft[] = input.clone();

		// The plan uses the number of complex elements in the input array, i.e:
		// length/2.
		cufftHandle plan = new cufftHandle();
		JCufft.cufftPlan1d(plan, input.length / 2, cufftType.CUFFT_C2C, 1);
		JCufft.cufftExecC2C(plan, outputJCufft, outputJCufft,
				JCufft.CUFFT_INVERSE);
		JCufft.cufftDestroy(plan);

		// Normalization allows for amplitude reconstruction of the original
		// signal.
		if (isNormalize) {
			float normalize = 2 / (float) outputJCufft.length;
			outputJCufft = CaxpyGpu.CaxpyFloatScalar(normalize, outputJCufft);
		}

		return outputJCufft;
	}

}

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)


Written By
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 do contract work for an environmental analytical laboratory, where I am primarily focused on LIMS programming and network administration.

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.

Comments and Discussions