CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2010
    Posts
    1

    Using Open MPI and CUDA in Windows 7

    I am trying to run a Simple CUDA program which queries the numbers of GPUs in the nodes using Open MPI v1.6.1 (64 bit) in Windows 7 Professional 64 bit Machines. When I run the application in root node, the correct GPU count is getting. But when the same application is tried to run in different nodes remotely using Open MPI, the GPU count is getting as 0, ie, it is showing as No CUDA capable GPU is present in the node.

    The root and the nodes having Compute Capability 2.1 capable GPUs. I have built the code with CUDA 4.2. When a normal MPI application is running, it is working correctly.

    Is there any special settings required for running a CUDA application in Windows 7 using Open MPI v1.6.1?

  2. #2
    Join Date
    Oct 2016
    Posts
    5

    Re: Using Open MPI and CUDA in Windows 7

    trying to do sample jcuda program of addition given on NVDIA's site: http://www.jcuda.org/samples/JCudaVectorAdd.java
    on windows(eclipse), for that I installed cuda 6.5 toolkit. Here is my program
    Code:
    
                    import static jcuda.driver.JCudaDriver.*;
                    import java.io.*;
                    import jcuda.*;
                    import jcuda.driver.*;
    
                    /**
                    * This is a sample class demonstrating how to use the JCuda driver
                    * bindings to load and execute a CUDA vector addition kernel.
                    * The sample reads a CUDA file, compiles it to a PTX file
                    * using NVCC, loads the PTX file as a module and executes
                    * the kernel function. <br />
                    */
                    public class TestSampleCuda
                    {
                        /**
                         * Entry point of this sample
                         *
                         * @param args Not used
                         * @throws IOException If an IO error occurs
                         */
                        public static void main(String args[]) throws IOException
                        {
                            // Enable exceptions and omit all subsequent error checks
                            JCudaDriver.setExceptionsEnabled(true);
    
                            // Create the PTX file by calling the NVCC
                           String ptxFileName = preparePtxFile("C:\\Users\\590943\\workspace\\Assignments\\TestSampleCudaKernel.cu");
                    //        String ptxFileName = "C:\\Users\\590943\\workspace\\Assignments\\JCudaVectorAddKernel.ptx";
                            // Initialize the driver and create a context for the first device.
                            cuInit(0);
                            CUdevice device = new CUdevice();
                            cuDeviceGet(device, 0);
                            CUcontext context = new CUcontext();
                            cuCtxCreate(context, 0, device);
    
                            // Load the ptx file.
                            CUmodule module = new CUmodule();
                            cuModuleLoad(module, ptxFileName);
    
                            // Obtain a function pointer to the "add" function.
                            CUfunction function = new CUfunction();
                            cuModuleGetFunction(function, module, "add");
    
                            int numElements = 1000000;
    
                            // Allocate and fill the host input data
                            float hostInputA[] = new float[numElements];
                            float hostInputB[] = new float[numElements];
                            for(int i = 0; i < numElements; i++)
                            {
                                hostInputA[i] = (float)i;
                                hostInputB[i] = (float)i;
                            }
    
                            // Allocate the device input data, and copy the
                            // host input data to the device
                            CUdeviceptr deviceInputA = new CUdeviceptr();
                            cuMemAlloc(deviceInputA, numElements * Sizeof.FLOAT);
                            cuMemcpyHtoD(deviceInputA, Pointer.to(hostInputA),
                                numElements * Sizeof.FLOAT);
                            CUdeviceptr deviceInputB = new CUdeviceptr();
                            cuMemAlloc(deviceInputB, numElements * Sizeof.FLOAT);
                            cuMemcpyHtoD(deviceInputB, Pointer.to(hostInputB),
                                numElements * Sizeof.FLOAT);
    
                            // Allocate device output memory
                            CUdeviceptr deviceOutput = new CUdeviceptr();
                            cuMemAlloc(deviceOutput, numElements * Sizeof.FLOAT);
    
                            // Set up the kernel parameters: A pointer to an array
                            // of pointers which point to the actual values.
                            Pointer kernelParameters = Pointer.to(
                                Pointer.to(new int[]{numElements}),
                                Pointer.to(deviceInputA),
                                Pointer.to(deviceInputB),
                                Pointer.to(deviceOutput)
                            );
    
                            // Call the kernel function.
                            int blockSizeX = 256;
                            int gridSizeX = (int)Math.ceil((double)numElements / blockSizeX);
                            cuLaunchKernel(function,
                                gridSizeX,  1, 1,      // Grid dimension
                                blockSizeX, 1, 1,      // Block dimension
                                0, null,               // Shared memory size and stream
                                kernelParameters, null // Kernel- and extra parameters
                            );
                            cuCtxSynchronize();
    
                            // Allocate host output memory and copy the device output
                            // to the host.
                            float hostOutput[] = new float[numElements];
                            cuMemcpyDtoH(Pointer.to(hostOutput), deviceOutput,
                                numElements * Sizeof.FLOAT);
    
                            // Verify the result
                            boolean passed = true;
                            for(int i = 0; i < numElements; i++)
                            {
                                float expected = i+i;
                                if (Math.abs(hostOutput[i] - expected) > 1e-5)
                                {
                                   System.out.println(
                                        "At index "+i+ " found "+hostOutput[i]+
                                        " but expected "+expected);
                                    passed = false;
                                    break;
                                }
                            }
                            System.out.println("Test "+(passed?"PASSED":"FAILED"));
    
                            // Clean up.
                            cuMemFree(deviceInputA);
                            cuMemFree(deviceInputB);
                            cuMemFree(deviceOutput);
                        }
    
                        /**
                         * The extension of the given file name is replaced with "ptx".
                         * If the file with the resulting name does not exist, it is
                         * compiled from the given file using NVCC. The name of the
                         * PTX file is returned.
                         *
                         * @param cuFileName The name of the .CU file
                         * @return The name of the PTX file
                         * @throws IOException If an I/O error occurs
                         */
                        private static String preparePtxFile(String cuFileName) throws IOException
                        {
                            int endIndex = cuFileName.lastIndexOf('.');
                            if (endIndex == -1)
                            {
                                endIndex = cuFileName.length()-1;
                            }
                            String ptxFileName = cuFileName.substring(0, endIndex+1)+"ptx";
                            
                            //File ptxFile = new File(ptxFileName);
                            
                            File ptxFile = new File(ptxFileName);
                            System.out.println(ptxFile.getCanonicalPath());
                            if (ptxFile.exists())
                            {
                                return ptxFileName;
                            }
    
                            File cuFile = new File(cuFileName);
                            System.out.println(cuFile.getCanonicalPath());
                            if (!cuFile.exists())
                            {
                                throw new IOException("Input file not found: "+cuFileName);
                            }
                            String modelString = "-m"+System.getProperty("sun.arch.data.model");
                            String command =
                                "nvcc " + modelString + " -ptx "+
                                cuFile.getPath()+" -o "+ptxFileName;
    
                            System.out.println("Executing\n"+command);
                            Process process = Runtime.getRuntime().exec(command);
    
                            String errorMessage =
                                new String(toByteArray(process.getErrorStream()));
                            String outputMessage =
                                new String(toByteArray(process.getInputStream()));
                            int exitValue = 0;
                            try
                            {
                                exitValue = process.waitFor();
                            }
                            catch (InterruptedException e)
                            {
                                Thread.currentThread().interrupt();
                                throw new IOException(
                                    "Interrupted while waiting for nvcc output", e);
                            }
    
                            if (exitValue != 0)
                            {
                                System.out.println("nvcc process exitValue "+exitValue);
                                System.out.println("errorMessage:\n"+errorMessage);
                                System.out.println("outputMessage:\n"+outputMessage);
                                throw new IOException(
                                    "Could not create .ptx file: "+errorMessage);
                            }
    
                            System.out.println("Finished creating PTX file");
                            return ptxFileName;
                        }
    
                        /**
                         * Fully reads the given InputStream and returns it as a byte array
                         *
                         * @param inputStream The input stream to read
                         * @return The byte array containing the data from the input stream
                         * @throws IOException If an I/O error occurs
                         */
                        private static byte[] toByteArray(InputStream inputStream)
                            throws IOException
                        {
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            byte buffer[] = new byte[8192];
                            while (true)
                            {
                                int read = inputStream.read(buffer);
                                if (read == -1)
                                {
                                    break;
                                }
                                baos.write(buffer, 0, read);
                            }
                            return baos.toByteArray();
                        }
    
    
                    }
    ***************************************************************************************************
    errors:

    C:\Users\590943\workspace\Assignments\TestSampleCudaKernel.ptx
    Exception in thread "main" C:\Users\590943\workspace\Assignments\TestSampleCudaKernel.cu
    java.io.IOException: Input file not found: C:\Users\590943\workspace\Assignments\TestSampleCudaKernel.cu
    at TestSampleCuda.preparePtxFile(TestSampleCuda.java:169)
    at TestSampleCuda.main(TestSampleCuda.java:46)
    Last edited by 2kaud; October 4th, 2016 at 03:36 AM. Reason: Code tags added

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: Using Open MPI and CUDA in Windows 7

    [Moved to Java Programming forum as Java code]

    When posting code, please use code tags. Go Advanced, select the formatted code and click '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured