view graal/com.oracle.graal.hotspot.ptx.test/src/com/oracle/graal/hotspot/ptx/test/PTXLaunchKernelTest.java @ 13626:d368f5556059

disabled new PTX tests if PTX GPU is not available
author Doug Simon <doug.simon@oracle.com>
date Mon, 13 Jan 2014 23:03:12 +0100
parents 220ed109bf77
children 1dabd01a73bd
line wrap: on
line source

/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code 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
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.graal.hotspot.ptx.test;

import static com.oracle.graal.api.code.CodeUtil.*;
import static com.oracle.graal.compiler.GraalCompiler.*;
import static com.oracle.graal.hotspot.HotSpotGraalRuntime.*;

import org.junit.*;

import com.oracle.graal.api.code.*;
import com.oracle.graal.api.code.CallingConvention.Type;
import com.oracle.graal.api.meta.*;
import com.oracle.graal.compiler.test.*;
import com.oracle.graal.debug.*;
import com.oracle.graal.debug.Debug.Scope;
import com.oracle.graal.hotspot.*;
import com.oracle.graal.hotspot.bridge.*;
import com.oracle.graal.hotspot.meta.*;
import com.oracle.graal.hotspot.ptx.*;
import com.oracle.graal.lir.asm.*;
import com.oracle.graal.nodes.*;
import com.oracle.graal.phases.*;
import com.oracle.graal.phases.tiers.*;
import com.oracle.graal.ptx.*;

/**
 * Tests the mechanism for launching a PTX kernel method via wrapper code generated by
 * {@link PTXLaunchKernelGraphKit}.
 */
public class PTXLaunchKernelTest extends GraalCompilerTest {

    public PTXLaunchKernelTest() {
        super();
    }

    /**
     * Compiles and installs PTX kernel code for a given method.
     */
    static class PTXKernel extends GraalCompilerTest {
        public PTXKernel() {
            super(PTX.class);
        }

        static CompilerToGPU toGPU = HotSpotGraalRuntime.runtime().getCompilerToGPU();
        static boolean validDevice = toGPU.deviceInit();

        @Override
        protected CompilationResult compile(ResolvedJavaMethod method, StructuredGraph graph) {
            Assume.assumeTrue(getBackend() instanceof PTXHotSpotBackend);

            /*
             * Use Suites.createDefaultSuites() instead of GraalCompilerTest.suites. The
             * GraalCompilerTest.suites variable contains the Suites for the HotSpotRuntime. This
             * code will not run on HotSpot, so it should use the plain Graal default suites,
             * without HotSpot specific phases.
             * 
             * Ultimately we might want to have both the kernel and the code natively compiled for
             * GPU fallback to CPU in cases of ECC failure on kernel invocation.
             */
            CallingConvention cc = getCallingConvention(getCodeCache(), Type.JavaCallee, graph.method(), false);
            Suites suites = Suites.createDefaultSuites();
            PTXHotSpotBackend ptxBackend = (PTXHotSpotBackend) getBackend();
            ExternalCompilationResult kernelResult = compileGraph(graph, cc, method, getProviders(), ptxBackend, ptxBackend.getTarget(), null, getDefaultGraphBuilderSuite(),
                            OptimisticOptimizations.NONE, getProfilingInfo(graph), new SpeculationLog(), suites, true, new ExternalCompilationResult(), CompilationResultBuilderFactory.Default);

            Assume.assumeTrue(validDevice);
            Assert.assertTrue(kernelResult.getTargetCode() != null);
            try (Scope ds = Debug.scope("GeneratingKernel")) {
                long kernel = toGPU.generateKernel(kernelResult.getTargetCode(), method.getName());
                kernelResult.setEntryPoint(kernel);
            } catch (Throwable e) {
                throw Debug.handle(e);
            }
            return kernelResult;
        }

        @Override
        protected InstalledCode addMethod(ResolvedJavaMethod method, CompilationResult compResult) {
            HotSpotCodeCacheProvider codeCache = (HotSpotCodeCacheProvider) getCodeCache();
            return codeCache.addExternalMethod(method, compResult);
        }

        /**
         * Compiles and installs PTX kernel code for {@code method}.
         */
        InstalledCode getKernelCode(ResolvedJavaMethod method, StructuredGraph graph) {
            return getCode(method, graph, true);
        }
    }

    @Override
    protected InstalledCode getCode(ResolvedJavaMethod method, StructuredGraph graph) {
        InstalledCode kernelCode = new PTXKernel().getKernelCode(method, graph);
        StructuredGraph launchKernel = new PTXLaunchKernelGraphKit(method, kernelCode.getStart(), runtime().getHostProviders()).getGraph();
        return super.getCode(method, launchKernel);
    }

    @Test
    public void testStaticIntKernel() {
        test("staticIntKernel", 'a', 42);
    }

    @Test
    public void testVirtualIntKernel() {
        test("virtualIntKernel", 'a', 42);
    }

    public static int staticIntKernel(char p0, int p1) {
        return p1 + p0;
    }

    public int virtualIntKernel(char p0, int p1) {
        return p1 + p0;
    }
}