<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Calling mkl_malloc in C# in Intel® oneAPI Math Kernel Library</title>
    <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767954#M397</link>
    <description>Thanks for the suggestion. I tried using &lt;BR /&gt;&lt;BR /&gt;public static extern void* mkl_malloc(Int64 size, int alignment);&lt;BR /&gt;&lt;BR /&gt;but I still get the same exception. I've been banging my head against a wall with this one. Any other suggestions?&lt;BR /&gt;&lt;BR /&gt;Ryan</description>
    <pubDate>Tue, 23 Aug 2011 13:11:28 GMT</pubDate>
    <dc:creator>Ryan_Deering</dc:creator>
    <dc:date>2011-08-23T13:11:28Z</dc:date>
    <item>
      <title>Calling mkl_malloc in C#</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767952#M395</link>
      <description>I am using the MKL libraries in C# to build an application that uses various BLAS, LAPACK, VML, and VSL functions in a 64-bit environment. I have been using the C# double[] array and the fixed keyword to call into the MKL functions. In profiling the code, I have found the CLR memory allocation is a significant portion of the calculation time. As an alternative, I am investigating the use of mkl_malloc. Unfortunately, I have been unable to successfully call into mkl_malloc. I get the following exception:&lt;BR /&gt;&lt;BR /&gt;Exception has been thrown by the target of an invocation.&lt;BR /&gt;Attempted to read or write protected memory. This is often an indication that other memory is corrupt.&lt;BR /&gt;&lt;BR /&gt;Can anyone give me advice on how to call into mkl_malloc from C# or whether this is impossible?&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;Ryan Deering&lt;BR /&gt;&lt;BR /&gt;Here is sample code that illustrates the issue:&lt;BR /&gt;&lt;BR /&gt;using System;&lt;BR /&gt;using System.Runtime.InteropServices;&lt;BR /&gt;using Microsoft.VisualStudio.TestTools.UnitTesting;&lt;BR /&gt;&lt;BR /&gt;namespace MKLMalloc&lt;BR /&gt;{&lt;BR /&gt; [TestClass]&lt;BR /&gt; public unsafe class UnitTest1&lt;BR /&gt; {&lt;BR /&gt; [TestMethod]&lt;BR /&gt; public void MallocFree()&lt;BR /&gt; {&lt;BR /&gt; void* ptr = MKL.mkl_malloc(1,8);&lt;BR /&gt; MKL.mkl_free(ptr);&lt;BR /&gt; }&lt;BR /&gt; }&lt;BR /&gt;&lt;BR /&gt; public static unsafe class MKL&lt;BR /&gt; {&lt;BR /&gt; private const string mkl = "mkl_rt.dll";&lt;BR /&gt;&lt;BR /&gt; [DllImport(mkl)]&lt;BR /&gt; public static extern void* mkl_malloc(Int32 size, int alignment);&lt;BR /&gt;&lt;BR /&gt; [DllImport(mkl)]&lt;BR /&gt; public static extern void mkl_free(void* ptr);&lt;BR /&gt; }&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 22 Aug 2011 21:08:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767952#M395</guid>
      <dc:creator>Ryan_Deering</dc:creator>
      <dc:date>2011-08-22T21:08:00Z</dc:date>
    </item>
    <item>
      <title>Calling mkl_malloc in C#</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767953#M396</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;MKL_malloc for C/C++ is declared as &lt;STRONG&gt;&lt;EM&gt;void *MKL_malloc(size_t size, int alignment)&lt;BR /&gt;&lt;/EM&gt;&lt;/STRONG&gt;so on Intel64 in should be used from C# as&lt;BR /&gt; &lt;STRONG&gt;&lt;EM&gt;public static extern void* mkl_malloc(Int64 size, int alignment);&lt;/EM&gt;&lt;/STRONG&gt;</description>
      <pubDate>Tue, 23 Aug 2011 04:41:15 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767953#M396</guid>
      <dc:creator>barragan_villanueva_</dc:creator>
      <dc:date>2011-08-23T04:41:15Z</dc:date>
    </item>
    <item>
      <title>Calling mkl_malloc in C#</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767954#M397</link>
      <description>Thanks for the suggestion. I tried using &lt;BR /&gt;&lt;BR /&gt;public static extern void* mkl_malloc(Int64 size, int alignment);&lt;BR /&gt;&lt;BR /&gt;but I still get the same exception. I've been banging my head against a wall with this one. Any other suggestions?&lt;BR /&gt;&lt;BR /&gt;Ryan</description>
      <pubDate>Tue, 23 Aug 2011 13:11:28 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767954#M397</guid>
      <dc:creator>Ryan_Deering</dc:creator>
      <dc:date>2011-08-23T13:11:28Z</dc:date>
    </item>
    <item>
      <title>Calling mkl_malloc in C#</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767955#M398</link>
      <description>Ryan,&lt;BR /&gt;&lt;BR /&gt;Could you please create small C# testcase to reproduce it on our side?&lt;BR /&gt;BTW, did you look at artticle: &lt;A href="http://software.intel.com/en-us/articles/using-intel-mkl-in-your-c-program/"&gt;Using Intel MKL in your C# program&lt;/A&gt;</description>
      <pubDate>Wed, 24 Aug 2011 13:12:10 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767955#M398</guid>
      <dc:creator>barragan_villanueva_</dc:creator>
      <dc:date>2011-08-24T13:12:10Z</dc:date>
    </item>
    <item>
      <title>Calling mkl_malloc in C#</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767956#M399</link>
      <description>Victor,&lt;BR /&gt;&lt;BR /&gt;Below is a test case using NUnit. My workaround for now is using Marshal.AllocHGlobal and Marshal.FreeHGlobal instead of mkl_malloc and mkl_free as can be seen in MarshalTest below. &lt;BR /&gt;&lt;BR /&gt;I have looked at the examples in the article you provided, but there are no examples of using mkl_malloc in C# that I could find. An example of why I want to use a malloc function is for random number generation. The default behavior of C# new double&lt;N&gt; is to fill the array with 0. When filling an array with random numbers with VSL, there is no need to fill it with zeroes first. Filling with zeros added around 40% overhead to random number generation compared to using Marshal.AllocHGlobal which allocates memory but does not initialize.&lt;BR /&gt;&lt;BR /&gt;Let me know if you need me to attach files or if the code snippet is not enough to investigate. I have the MKL redistributables in my PATH and use NUnit available at &lt;A href="http://www.nunit.org/?p=download" target="_blank"&gt;http://www.nunit.org/?p=download&lt;/A&gt;. The test runner also needs to be a 64-bit executable. I am using MKL version 10.3.2.1. &lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;Ryan&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;---------------------------------------&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;using System;&lt;BR /&gt;using System.Runtime.InteropServices;&lt;BR /&gt;using NUnit.Framework;&lt;BR /&gt;&lt;BR /&gt;namespace MKLMalloc&lt;BR /&gt;{&lt;BR /&gt; [TestFixture]&lt;BR /&gt; public class UnitTest2&lt;BR /&gt; {&lt;BR /&gt; [Test]&lt;BR /&gt; public unsafe void MallocFree()&lt;BR /&gt; {&lt;BR /&gt; Int64 size = 1000;&lt;BR /&gt; void* ptr = MKL_NUnit.mkl_malloc(size*sizeof(double), 8); //fails&lt;BR /&gt; MKL_NUnit.mkl_free(ptr);&lt;BR /&gt; }&lt;BR /&gt;&lt;BR /&gt; [Test]&lt;BR /&gt; public unsafe void MarshalTest()&lt;BR /&gt; {&lt;BR /&gt; int i = 1000;&lt;BR /&gt; IntPtr ptr = Marshal.AllocHGlobal(i*sizeof(double)); //works&lt;BR /&gt; Marshal.FreeHGlobal(ptr);&lt;BR /&gt; }&lt;BR /&gt;&lt;BR /&gt; }&lt;BR /&gt;&lt;BR /&gt; public static unsafe class MKL_NUnit&lt;BR /&gt; {&lt;BR /&gt; private const string mkl = "mkl_rt.dll";&lt;BR /&gt;&lt;BR /&gt; [DllImport(mkl)]&lt;BR /&gt; public static extern void* mkl_malloc(Int64 size, int alignment);&lt;BR /&gt;&lt;BR /&gt; [DllImport(mkl)]&lt;BR /&gt; public static extern void mkl_free(void* ptr);&lt;BR /&gt; }&lt;BR /&gt;}&lt;/N&gt;</description>
      <pubDate>Wed, 24 Aug 2011 17:51:50 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767956#M399</guid>
      <dc:creator>Ryan_Deering</dc:creator>
      <dc:date>2011-08-24T17:51:50Z</dc:date>
    </item>
    <item>
      <title>Calling mkl_malloc in C#</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767957#M400</link>
      <description>&lt;P&gt;Ryan,&lt;/P&gt;&lt;P&gt;The correct way of using memory functions in C# is to call MKL_malloc and MKL_free.&lt;/P&gt;&lt;P&gt;Though calling mkl_malloc/mkl_free is ok for C (&lt;A href="http://software.intel.com/sites/products/documentation/hpc/composerxe/en-us/mklxe/mkl_userguide_lnx/MKL_UG_coding_tips/Aligning_Data_for_Numerical_Stability.htm"&gt;see example&lt;/A&gt;), these names are just defines in the header file mkl_service.h:&lt;BR /&gt;&lt;BR /&gt;#define mkl_malloc MKL_malloc&lt;BR /&gt;#define mkl_free MKL_free&lt;BR /&gt;&lt;BR /&gt;You can also use mkl_malloc/mkl_free but with Fortran calling conventions  as references.&lt;BR /&gt;&lt;BR /&gt;Ive prepared a small suite with the both approaches. It is based on the example files from the article &lt;A href="http://software.intel.com/en-us/articles/using-intel-mkl-in-your-c-program/"&gt;Using Intel MKL in your C# program&lt;/A&gt;.&lt;BR /&gt;&lt;BR /&gt;&lt;A onclick="ndownload('http://software.intel.com/file/38289')"&gt;mkl_malloc.zip&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;Vladimir&lt;/P&gt;</description>
      <pubDate>Mon, 29 Aug 2011 11:02:55 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767957#M400</guid>
      <dc:creator>Vladimir_Koldakov__I</dc:creator>
      <dc:date>2011-08-29T11:02:55Z</dc:date>
    </item>
    <item>
      <title>Calling mkl_malloc in C#</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767958#M401</link>
      <description>Vladimir,&lt;BR /&gt;&lt;BR /&gt;I got my tests working after seeing the differences between the utility functions that use MKL_ and mkl_. &lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;BR /&gt;Ryan</description>
      <pubDate>Fri, 02 Sep 2011 01:44:29 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/Calling-mkl-malloc-in-C/m-p/767958#M401</guid>
      <dc:creator>Ryan_Deering</dc:creator>
      <dc:date>2011-09-02T01:44:29Z</dc:date>
    </item>
  </channel>
</rss>

