8000 Format combined memory-handle and graphics-registration flags by RicheyWorks · Pull Request #57 · jcuda/jcuda · GitHub
[go: up one dir, main page]

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ private CUmemAllocationHandleType()
}

/**
* Returns a string representation of the given constant
* Returns a string representation of the given constant or flag combination.
* This describes the bits, not whether a CUDA operation supports them.
*
* @return A string representation of the given constant
* @param n The constant or flag combination
* @return A string representation of the given constant or flag combination
*/
public static String stringFor(int n)
{
Expand All @@ -72,6 +74,19 @@ public static String stringFor(int n)
case CU_MEM_HANDLE_TYPE_WIN32_KMT: return "CU_MEM_HANDLE_TYPE_WIN32_KMT";
case CU_MEM_HANDLE_TYPE_MAX: return "CU_MEM_HANDLE_TYPE_MAX";
}
// Keep special constants above and reject bits unknown to this binding.
int knownFlags =
CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR |
CU_MEM_HANDLE_TYPE_WIN32 |
CU_MEM_HANDLE_TYPE_WIN32_KMT;
if ((n & ~knownFlags) == 0)
{
String result = "";
if ((n & CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR) != 0) result += "CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR ";
if ((n & CU_MEM_HANDLE_TYPE_WIN32) != 0) result += "CU_MEM_HANDLE_TYPE_WIN32 ";
if ((n & CU_MEM_HANDLE_TYPE_WIN32_KMT) != 0) result += "CU_MEM_HANDLE_TYPE_WIN32_KMT ";
return result.trim();
}
return "INVALID CUmemAllocationHandleType: "+n;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public class cudaGraphicsRegisterFlags
public static final int cudaGraphicsRegisterFlagsTextureGather = 8;

/**
* Returns the String identifying the given cudaGraphicsRegisterFlags
* Returns the String identifying the given cudaGraphicsRegisterFlags combination.
* This describes the bits, not whether a CUDA operation supports them.
*
* @param n The cudaGraphicsRegisterFlags
* @return The String identifying the given cudaGraphicsRegisterFlags
Expand All @@ -73,6 +74,21 @@ public static String stringFor(int n)
case cudaGraphicsRegisterFlagsSurfaceLoadStore: return "cudaGraphicsRegisterFlagsSurfaceLoadStore";
case cudaGraphicsRegisterFlagsTextureGather: return "cudaGraphicsRegisterFlagsTextureGather";
}
// Keep special constants above and reject bits unknown to this binding.
int knownFlags =
cudaGraphicsRegisterFlagsReadOnly |
cudaGraphicsRegisterFlagsWriteDiscard |
cudaGraphicsRegisterFlagsSurfaceLoadStore |
cudaGraphicsRegisterFlagsTextureGather;
if ((n & ~knownFlags) == 0)
{
String result = "";
if ((n & cudaGraphicsRegisterFlagsReadOnly) != 0) result += "cudaGraphicsRegisterFlagsReadOnly ";
if ((n & cudaGraphicsRegisterFlagsWriteDiscard) != 0) result += "cudaGraphicsRegisterFlagsWriteDiscard ";
if ((n & cudaGraphicsRegisterFlagsSurfaceLoadStore) != 0) result += "cudaGraphicsRegisterFlagsSurfaceLoadStore ";
if ((n & cudaGraphicsRegisterFlagsTextureGather) != 0) result += "cudaGraphicsRegisterFlagsTextureGather ";
return result.trim();
}
return "INVALID cudaGraphicsRegisterFlags: "+n;
}

Expand Down
75 changes: 75 additions & 0 deletions JCudaJava/src/test/java/jcuda/test/FlagCombinationStringTest.java
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package jcuda.test;

import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import jcuda.driver.CUmemAllocationHandleType;
import jcuda.runtime.cudaGraphicsRegisterFlags;

/**
* Diagnostic formatting only: a named bit combination is not necessarily
* supported by a particular CUDA API, resource or platform.
*/
@RunWith(Parameterized.class)
public class FlagCombinationStringTest
{
@Parameterized.Parameters(name = "{0}[{1}]")
public static Collection<Object[]> cases()
{
return Arrays.asList(new Object[][] {
{"handle", 0, "CU_MEM_HANDLE_TYPE_NONE"},
{"handle", 1, "CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR"},
{"handle", 2, "CU_MEM_HANDLE_TYPE_WIN32"},
{"handle", 3, "CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR CU_MEM_HANDLE_TYPE_WIN32"},
{"handle", 4, "CU_MEM_HANDLE_TYPE_WIN32_KMT"},
{"handle", 5, "CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR CU_MEM_HANDLE_TYPE_WIN32_KMT"},
{"handle", 6, "CU_MEM_HANDLE_TYPE_WIN32 CU_MEM_HANDLE_TYPE_WIN32_KMT"},
{"handle", 7, "CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR CU_MEM_HANDLE_TYPE_WIN32 CU_MEM_HANDLE_TYPE_WIN32_KMT"},
{"graphics", 0, "cudaGraphicsRegisterFlagsNone"},
{"graphics", 1, "cudaGraphicsRegisterFlagsReadOnly"},
{"graphics", 2, "cudaGraphicsRegisterFlagsWriteDiscard"},
{"graphics", 3, "cudaGraphicsRegisterFlagsReadOnly cudaGraphicsRegisterFlagsWriteDiscard"},
{"graphics", 4, "cudaGraphicsRegisterFlagsSurfaceLoadStore"},
{"graphics", 5, "cudaGraphicsRegisterFlagsReadOnly cudaGraphicsRegisterFlagsSurfaceLoadStore"},
{"graphics", 6, "cudaGraphicsRegisterFlagsWriteDiscard cudaGraphicsRegisterFlagsSurfaceLoadStore"},
{"graphics", 7, "cudaGraphicsRegisterFlagsReadOnly cudaGraphicsRegisterFlagsWriteDiscard cudaGraphicsRegisterFlagsSurfaceLoadStore"},
{"graphics", 8, "cudaGraphicsRegisterFlagsTextureGather"},
{"graphics", 9, "cudaGraphicsRegisterFlagsReadOnly cudaGraphicsRegisterFlagsTextureGather"},
{"graphics", 10, "cudaGraphicsRegisterFlagsWriteDiscard cudaGraphicsRegisterFlagsTextureGather"},
{"graphics", 11, "cudaGraphicsRegisterFlagsReadOnly cudaGraphicsRegisterFlagsWriteDiscard cudaGraphicsRegisterFlagsTextureGather"},
{"graphics", 12, "cudaGraphicsRegisterFlagsSurfaceLoadStore cudaGraphicsRegisterFlagsTextureGather"},
{"graphics", 13, "cudaGraphicsRegisterFlagsReadOnly cudaGraphicsRegisterFlagsSurfaceLoadStore cudaGraphicsRegisterFlagsTextureGather"},
{"graphics", 14, "cudaGraphicsRegisterFlagsWriteDiscard cudaGraphicsRegisterFlagsSurfaceLoadStore cudaGraphicsRegisterFlagsTextureGather"},
{"graphics", 15, "cudaGraphicsRegisterFlagsReadOnly cudaGraphicsRegisterFlagsWriteDiscard cudaGraphicsRegisterFlagsSurfaceLoadStore cudaGraphicsRegisterFlagsTextureGather"},
{"handle", -1, "CU_MEM_HANDLE_TYPE_MAX"},
{"handle", 8, "INVALID CUmemAllocationHandleType: 8"},
{"handle", 9, "INVALID CUmemAllocationHandleType: 9"},
{"graphics", 16, "INVALID cudaGraphicsRegisterFlags: 16"},
{"graphics", 17, "INVALID cudaGraphicsRegisterFlags: 17"},
{"graphics", -1, "INVALID cudaGraphicsRegisterFlags: -1"}
});
}

private final String kind;
private final int value;
private final String expected;

public FlagCombinationStringTest(String kind, int value, String expected)
{
this.kind = kind;
this.value = value;
this.expected = expected;
}

@Test
public void formatsKnownBitsAndPreservesSpecialValues()
{
String actual = "handle".equals(kind)
? CUmemAllocationHandleType.stringFor(value)
: cudaGraphicsRegisterFlags.stringFor(value);
assertEquals(expected, actual);
}
}
0