472,958 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Memory corruption with interop call

First of all, I apologize for cross-posting. I posted this on the
framework.interop group as well, but haven't received a response yet, so I
thought I'd try here...

I'm trying to write an app that perfroms some work using the CUDA SDK (for
programming the nVidia GPU).

One of the first methods I wrote is simply to query information about the
CUDA devices (video cards).

The code is very straightforward, but I'm getting an error that indicates
some sort of memory corruption and I'm not really sure how it's happening. I
don't think it's CUDA related, I think it's just something wrong with my
interop calls. Maybe the arrays? The exception I'm getting happens during
the return from GetCUDADeviceProperties (see implementation below and
comment on line that throws exception). It appears to be happening in the
marshalling back of the data. The exception is:

"An unhandled exception of type 'System.AccessViolationException' occurred
in NeuralNetTimingTest.exe
Additional information: Attempted to read or write protected memory. This is
often an indication that other memory is corrupt."

When I step through the code, GetCUDADeviceProperties() appears to properly
set the fields in the CUDADevice structure.

Here's the relevant code:

From C#:

* The device information structure is this:

[StructLayout(LayoutKind.Sequential)]
public struct CUDADevice
{
public int deviceID;
public int totalMem;
public int numMultiProcessors;
public int numCores;
public int constantMem;
public int sharedMem;
public int registersPerBlock;
public int warpSize;
public int numThreadsPerBlock;
public int[] maxBlockDimensions;
public int[] maxGridDimensions;
}

* The DllImport is:

[DllImport("NeuralNetCUDALib")]
extern static bool GetCUDADeviceProperties(ref CUDADevice device);

* And the C# code that calls it is:

// Allocate the device structure
CUDADevice dev = new CUDADevice();
// Allocate the two arrays
dev.maxBlockDimensions = new int[3];
dev.maxGridDimensions = new int[3];

// Device to query
dev.deviceID = 0;
GetCUDADeviceProperties(ref dev);

* The non-CUDA C++ code is:
* From the .h, the C++ side of the CUDADevice structure

typedef struct tagCUDADevice
{
int deviceID;
int totalMem;
int numMultiProcessors;
int numCores;
int constantMem;
int sharedMem;
int registersPerBlock;
int warpSize;
int numThreadsPerBlock;
int maxBlockDimensions[3];
int maxGridDimensions[3];
} CUDADevice;

extern "C" bool GetCUDADeviceProperties(CUDADevice *pDevice)
{
if (GetDeviceCount() <= 0)
{
return false;
}

if (!QueryDeviceInfo(pDevice->deviceID,
pDevice->totalMem,
pDevice->numMultiProcessors,
pDevice->numCores,
pDevice->constantMem,
pDevice->sharedMem,
pDevice->registersPerBlock,
pDevice->warpSize,
pDevice->numThreadsPerBlock,
pDevice->maxBlockDimensions,
pDevice->maxGridDimensions))
{
return false;
}
return true; // Exception happens during this return!
}
* And the actual CUDA code is:

bool QueryDeviceInfo(int deviceNum,
int &totalMem,
int &numMultiProcessors,
int &numCores,
int &constantMem,
int &sharedMem,
int &registersPerBlock,
int &warpSize,
int &numThreadsPerBlock,
int *maxBlockDimensions,
int *maxGridDimensions)
{
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, deviceNum);
totalMem = (int) prop.totalGlobalMem;
numMultiProcessors = (int) prop.multiProcessorCount;
numCores = (int) numMultiProcessors * 8;
constantMem = (int) prop.totalConstMem;
sharedMem = (int) prop.sharedMemPerBlock;
registersPerBlock = (int) prop.regsPerBlock;
warpSize = (int) prop.warpSize;
numThreadsPerBlock = (int) prop.maxThreadsPerBlock;
maxBlockDimensions[0] = (int) prop.maxThreadsDim[0];
maxBlockDimensions[1] = (int) prop.maxThreadsDim[1];
maxBlockDimensions[2] = (int) prop.maxThreadsDim[2];
maxGridDimensions[0] = (int) prop.maxGridSize[0];
maxGridDimensions[1] = (int) prop.maxGridSize[1];
maxGridDimensions[2] = (int) prop.maxGridSize[2];
return true;
}

Jul 28 '08 #1
2 3707
Fredo,

It looks like your declaration for the CUDADevice is incorrect. You
have to specify that the arrays are passed by value in the structure of the
array:

[StructLayout(LayoutKind.Sequential)]
public struct CUDADevice
{
public int deviceID;
public int totalMem;
public int numMultiProcessors;
public int numCores;
public int constantMem;
public int sharedMem;
public int registersPerBlock;
public int warpSize;
public int numThreadsPerBlock;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public int[] maxBlockDimensions;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public int[] maxGridDimensions;
}

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Fredo" <fr***@hotmail.comwrote in message
news:_c******************************@giganews.com ...
First of all, I apologize for cross-posting. I posted this on the
framework.interop group as well, but haven't received a response yet, so I
thought I'd try here...

I'm trying to write an app that perfroms some work using the CUDA SDK (for
programming the nVidia GPU).

One of the first methods I wrote is simply to query information about the
CUDA devices (video cards).

The code is very straightforward, but I'm getting an error that indicates
some sort of memory corruption and I'm not really sure how it's happening.
I
don't think it's CUDA related, I think it's just something wrong with my
interop calls. Maybe the arrays? The exception I'm getting happens during
the return from GetCUDADeviceProperties (see implementation below and
comment on line that throws exception). It appears to be happening in the
marshalling back of the data. The exception is:

"An unhandled exception of type 'System.AccessViolationException' occurred
in NeuralNetTimingTest.exe
Additional information: Attempted to read or write protected memory. This
is
often an indication that other memory is corrupt."

When I step through the code, GetCUDADeviceProperties() appears to
properly
set the fields in the CUDADevice structure.

Here's the relevant code:

From C#:

* The device information structure is this:

[StructLayout(LayoutKind.Sequential)]
public struct CUDADevice
{
public int deviceID;
public int totalMem;
public int numMultiProcessors;
public int numCores;
public int constantMem;
public int sharedMem;
public int registersPerBlock;
public int warpSize;
public int numThreadsPerBlock;
public int[] maxBlockDimensions;
public int[] maxGridDimensions;
}

* The DllImport is:

[DllImport("NeuralNetCUDALib")]
extern static bool GetCUDADeviceProperties(ref CUDADevice device);

* And the C# code that calls it is:

// Allocate the device structure
CUDADevice dev = new CUDADevice();
// Allocate the two arrays
dev.maxBlockDimensions = new int[3];
dev.maxGridDimensions = new int[3];

// Device to query
dev.deviceID = 0;
GetCUDADeviceProperties(ref dev);

* The non-CUDA C++ code is:
* From the .h, the C++ side of the CUDADevice structure

typedef struct tagCUDADevice
{
int deviceID;
int totalMem;
int numMultiProcessors;
int numCores;
int constantMem;
int sharedMem;
int registersPerBlock;
int warpSize;
int numThreadsPerBlock;
int maxBlockDimensions[3];
int maxGridDimensions[3];
} CUDADevice;

extern "C" bool GetCUDADeviceProperties(CUDADevice *pDevice)
{
if (GetDeviceCount() <= 0)
{
return false;
}

if (!QueryDeviceInfo(pDevice->deviceID,
pDevice->totalMem,
pDevice->numMultiProcessors,
pDevice->numCores,
pDevice->constantMem,
pDevice->sharedMem,
pDevice->registersPerBlock,
pDevice->warpSize,
pDevice->numThreadsPerBlock,
pDevice->maxBlockDimensions,
pDevice->maxGridDimensions))
{
return false;
}
return true; // Exception happens during this return!
}
* And the actual CUDA code is:

bool QueryDeviceInfo(int deviceNum,
int &totalMem,
int &numMultiProcessors,
int &numCores,
int &constantMem,
int &sharedMem,
int &registersPerBlock,
int &warpSize,
int &numThreadsPerBlock,
int *maxBlockDimensions,
int *maxGridDimensions)
{
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, deviceNum);
totalMem = (int) prop.totalGlobalMem;
numMultiProcessors = (int) prop.multiProcessorCount;
numCores = (int) numMultiProcessors * 8;
constantMem = (int) prop.totalConstMem;
sharedMem = (int) prop.sharedMemPerBlock;
registersPerBlock = (int) prop.regsPerBlock;
warpSize = (int) prop.warpSize;
numThreadsPerBlock = (int) prop.maxThreadsPerBlock;
maxBlockDimensions[0] = (int) prop.maxThreadsDim[0];
maxBlockDimensions[1] = (int) prop.maxThreadsDim[1];
maxBlockDimensions[2] = (int) prop.maxThreadsDim[2];
maxGridDimensions[0] = (int) prop.maxGridSize[0];
maxGridDimensions[1] = (int) prop.maxGridSize[1];
maxGridDimensions[2] = (int) prop.maxGridSize[2];
return true;
}

Jul 28 '08 #2
You nailed it. Thanks Nicholas. Don't know what I'd do without you and
Jon....
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:OO**************@TK2MSFTNGP06.phx.gbl...
Fredo,

It looks like your declaration for the CUDADevice is incorrect. You
have to specify that the arrays are passed by value in the structure of
the array:

[StructLayout(LayoutKind.Sequential)]
public struct CUDADevice
{
public int deviceID;
public int totalMem;
public int numMultiProcessors;
public int numCores;
public int constantMem;
public int sharedMem;
public int registersPerBlock;
public int warpSize;
public int numThreadsPerBlock;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public int[] maxBlockDimensions;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public int[] maxGridDimensions;
}

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Fredo" <fr***@hotmail.comwrote in message
news:_c******************************@giganews.com ...
>First of all, I apologize for cross-posting. I posted this on the
framework.interop group as well, but haven't received a response yet, so
I thought I'd try here...

I'm trying to write an app that perfroms some work using the CUDA SDK
(for
programming the nVidia GPU).

One of the first methods I wrote is simply to query information about the
CUDA devices (video cards).

The code is very straightforward, but I'm getting an error that indicates
some sort of memory corruption and I'm not really sure how it's
happening. I
don't think it's CUDA related, I think it's just something wrong with my
interop calls. Maybe the arrays? The exception I'm getting happens during
the return from GetCUDADeviceProperties (see implementation below and
comment on line that throws exception). It appears to be happening in the
marshalling back of the data. The exception is:

"An unhandled exception of type 'System.AccessViolationException'
occurred
in NeuralNetTimingTest.exe
Additional information: Attempted to read or write protected memory. This
is
often an indication that other memory is corrupt."

When I step through the code, GetCUDADeviceProperties() appears to
properly
set the fields in the CUDADevice structure.

Here's the relevant code:

From C#:

* The device information structure is this:

[StructLayout(LayoutKind.Sequential)]
public struct CUDADevice
{
public int deviceID;
public int totalMem;
public int numMultiProcessors;
public int numCores;
public int constantMem;
public int sharedMem;
public int registersPerBlock;
public int warpSize;
public int numThreadsPerBlock;
public int[] maxBlockDimensions;
public int[] maxGridDimensions;
}

* The DllImport is:

[DllImport("NeuralNetCUDALib")]
extern static bool GetCUDADeviceProperties(ref CUDADevice device);

* And the C# code that calls it is:

// Allocate the device structure
CUDADevice dev = new CUDADevice();
// Allocate the two arrays
dev.maxBlockDimensions = new int[3];
dev.maxGridDimensions = new int[3];

// Device to query
dev.deviceID = 0;
GetCUDADeviceProperties(ref dev);

* The non-CUDA C++ code is:
* From the .h, the C++ side of the CUDADevice structure

typedef struct tagCUDADevice
{
int deviceID;
int totalMem;
int numMultiProcessors;
int numCores;
int constantMem;
int sharedMem;
int registersPerBlock;
int warpSize;
int numThreadsPerBlock;
int maxBlockDimensions[3];
int maxGridDimensions[3];
} CUDADevice;

extern "C" bool GetCUDADeviceProperties(CUDADevice *pDevice)
{
if (GetDeviceCount() <= 0)
{
return false;
}

if (!QueryDeviceInfo(pDevice->deviceID,
pDevice->totalMem,
pDevice->numMultiProcessors,
pDevice->numCores,
pDevice->constantMem,
pDevice->sharedMem,
pDevice->registersPerBlock,
pDevice->warpSize,
pDevice->numThreadsPerBlock,
pDevice->maxBlockDimensions,
pDevice->maxGridDimensions))
{
return false;
}
return true; // Exception happens during this return!
}
* And the actual CUDA code is:

bool QueryDeviceInfo(int deviceNum,
int &totalMem,
int &numMultiProcessors,
int &numCores,
int &constantMem,
int &sharedMem,
int &registersPerBlock,
int &warpSize,
int &numThreadsPerBlock,
int *maxBlockDimensions,
int *maxGridDimensions)
{
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, deviceNum);
totalMem = (int) prop.totalGlobalMem;
numMultiProcessors = (int) prop.multiProcessorCount;
numCores = (int) numMultiProcessors * 8;
constantMem = (int) prop.totalConstMem;
sharedMem = (int) prop.sharedMemPerBlock;
registersPerBlock = (int) prop.regsPerBlock;
warpSize = (int) prop.warpSize;
numThreadsPerBlock = (int) prop.maxThreadsPerBlock;
maxBlockDimensions[0] = (int) prop.maxThreadsDim[0];
maxBlockDimensions[1] = (int) prop.maxThreadsDim[1];
maxBlockDimensions[2] = (int) prop.maxThreadsDim[2];
maxGridDimensions[0] = (int) prop.maxGridSize[0];
maxGridDimensions[1] = (int) prop.maxGridSize[1];
maxGridDimensions[2] = (int) prop.maxGridSize[2];
return true;
}


Jul 28 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pete | last post by:
I've run into an interesting memory problem. I think I'm running out of heap space, but I'm not sure.... I'm creating two new arrays like such.... pImage = new UBYTE ; pImageTemp = new...
5
by: Trokey | last post by:
I am making interop calls to an object in a .NET component from a C++ program and am leaking memory... the following is some sample code: //////////////////////////////////////// // .NET...
5
by: Noa Garnett | last post by:
I'm developing on C++, using visual studio 6.0 with service pack 5. I have a memory corruption while debugging. Some of the variables I'm using are suddenly set to zero while progressing along the...
10
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
14
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005 and .net 2.0. I'm creating an application that has 3 forms. I want allow users to move forward and backward with the forms and retain the data users have entered. I thought...
5
by: John | last post by:
I would like to ask a question that is obvious to all people porting applications from the "traditional" C\VB6 interop scheme choosing C# vs VB.NET. We have a math library in C which...
3
by: not_a_commie | last post by:
The CLR won't garbage collect until it needs to. You should see the memory usage climb for some time before stabilizing. Can you change your declaration to use the 'out' keyword rather than a 'ref'...
66
by: Why Tea | last post by:
typedef struct some_struct { int i; short k, int m; char s; } some_struct_t; Assuming 16 bit or 32-bit alignment, can I assume that s always gets 4 or 8 bytes of allocation due to padding
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.