Skip to content

Commit

Permalink
Merge pull request #727 from Bertk/cuda
Browse files Browse the repository at this point in the history
support cudaKernel grammar
  • Loading branch information
guwirth committed Jan 2, 2016
2 parents 7089088 + f708fbe commit b332b89
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public enum CxxGrammarImpl implements GrammarRuleKey {
par_expression,
expression,
constantExpression,
cudaKernel,

// Statements
statement,
Expand Down Expand Up @@ -469,10 +470,10 @@ private static void expressions(LexerfulGrammarBuilder b) {

b.rule(postfixExpression).is(
b.firstOf(
b.sequence(simpleTypeSpecifier, "(", b.optional(expressionList), ")"),
b.sequence(simpleTypeSpecifier, b.optional(cudaKernel), "(", b.optional(expressionList), ")"),
b.sequence(simpleTypeSpecifier, bracedInitList),
b.sequence(simpleTypeSpecifier, "::", "typeid"),
b.sequence(typenameSpecifier, "(", b.optional(expressionList), ")"),
b.sequence(typenameSpecifier, b.optional(cudaKernel), "(", b.optional(expressionList), ")"),
b.sequence(typenameSpecifier, bracedInitList),
b.sequence(typenameSpecifier, "::", "typeid"),

Expand Down Expand Up @@ -511,6 +512,8 @@ private static void expressions(LexerfulGrammarBuilder b) {
)
).skipIfOneChild();

b.rule(cudaKernel).is(b.sequence("<<", "<", b.optional(expressionList), ">>", ">"));

b.rule(typeIdEnclosed).is(b.firstOf(
b.sequence("<", typeId, ">"),
b.sequence("<", innerTypeId, ">>")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

public class CxxParserTest extends ParserBaseTest {
String errSources = "/parser/bad/error_recovery_declaration.cc";
String[] goodFiles = {"own", "examples", "cli"};
String[] goodFiles = {"own", "examples", "cli", "cuda"};
String[] cCompatibilityFiles = {"c-compat"};
String rootDir = "src/test/resources/parser";
File erroneousSources = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public void postfixExpression() {
g.rule(CxxGrammarImpl.typeId).mock();
g.rule(CxxGrammarImpl.deleteExpression).mock();
g.rule(CxxGrammarImpl.pseudoDestructorName).mock();
g.rule(CxxGrammarImpl.cudaKernel).mock();

assertThat(p).matches("primaryExpression");
assertThat(p).matches("primaryExpression [ expression ]");
Expand All @@ -175,6 +176,7 @@ public void postfixExpression() {
assertThat(p).matches("simpleTypeSpecifier :: typeid");
assertThat(p).matches("typenameSpecifier :: typeid");

assertThat(p).matches("simpleTypeSpecifier cudaKernel ( expressionList )");
}

@Test
Expand All @@ -189,6 +191,8 @@ public void postfixExpression_reallife() {

assertThat(p).matches("G::typeid");
assertThat(p).matches("int::typeid");

assertThat(p).matches("kernel<<<gridDim,blockDim,0>>>(d_data, height, width)");
}

@Test
Expand Down
11 changes: 11 additions & 0 deletions cxx-squid/src/test/resources/parser/cuda/cuda-defines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// define project specific values to reduce parsing errors
#define __global__
#define __host__
#define __device__
#define __noinline__
#define __forceinline__
#define __constant__
#define __shared__
#dfeine __managed__
#define __restrict__
#define __align__(a)
41 changes: 41 additions & 0 deletions cxx-squid/src/test/resources/parser/cuda/kernel_function.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "cuda-defines.h"

texture<float, 2, cudaReadModeElementType> tex;

void foo()
{
cudaArray* cu_array;

// Allocate array
cudaChannelFormatDesc description = cudaCreateChannelDesc<float>();
cudaMallocArray(&cu_array, &description, width, height);

// Copy image data to array
cudaMemcpyToArray(cu_array, image, width*height*sizeof(float), cudaMemcpyHostToDevice);

// Set texture parameters (default)
tex.addressMode[0] = cudaAddressModeClamp;
tex.addressMode[1] = cudaAddressModeClamp;
tex.filterMode = cudaFilterModePoint;
tex.normalized = false; // do not normalize coordinates

// Bind the array to the texture
cudaBindTextureToArray(tex, cu_array);

// Run kernel
dim3 blockDim(16, 16, 1);
dim3 gridDim((width + blockDim.x - 1)/ blockDim.x, (height + blockDim.y - 1) / blockDim.y, 1);
kernel<<< gridDim, blockDim, 0 >>>(d_data, height, width);
// Unbind the array from the texture
cudaUnbindTexture(tex);
} //end foo()

__global__ void kernel(float* odata, int height, int width)
{
unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;
unsigned int y = blockIdx.y*blockDim.y + threadIdx.y;
if (x < width && y < height) {
float c = tex2D(tex, x, y);
odata[y*width+x] = c;
}
}

0 comments on commit b332b89

Please sign in to comment.