Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

VSL in C#

Janene_Pappas-mccril
829 Views

I'm looking for an example wrapper for the VSL functions and not finding one. The examples referred to in this article do not work: http://software.intel.com/en-us/articles/using-intel-mkl-in-your-c-program/

I'm specifically looking for how to use MKL to calculation the standard deviation/RMS of the elements of a vector - I think the functions are in the VSL?

Thanks in advance,

Janene

0 Kudos
1 Solution
Andrey_N_Intel
Employee
829 Views
Hi Janene, Can you please clarify the structure of your dataset in the example? Which of the variants below is correct: 1. the dimension of your random vector is 1 and number of observations 9 or 2. the dimension of your random vector is 9 and number of observations is 1? If you assume the first case please set task dimension p to 1 and number of observations n to vector.Length. If you assume the second case (that is, you have just one observation of 9d vector) the estimates are expected to be of size 9, and you need to allocate arrays of size 9 for mean/min/max/mom (min and max arrays should be properly initialized) Another observation is that in the signature vsldSSCompute([In] IntPtr task, [In] int estimates, [In] int method); you use 32 bit integer for the parameter "estimates" while in Intel(R) MKL this parameter is 64-bit integer (this, however, is not expected to impact on the result in your example). Please, let me know which result you have after the fixes on your side. Thanks, Andrey

View solution in original post

0 Kudos
9 Replies
Zhang_Z_Intel
Employee
829 Views
Intel MKL does not provide functions to compute RMS and standard deviation directly. But the VSL component already provides high performance functions to compute the second order raw algebraic moments and the variance (a.k.a. second order central moments). With these, it's really straightforward to roll your own RMS/standard deviation functions: • Compute the 2nd order raw moment (passing VSL_SS_ED_2R_MOM to vslSSEditTask), and then take the sqrt of it to get RMS. • Compute the variance (passing VSL_SS_ED_2C_MOM to vslSSEditTask), and then take the sqrt of it to get standard deviation. Let me know if this helps.
0 Kudos
Janene_Pappas-mccril
829 Views
Thanks Zhang. Do you have an example of a wrapper for VSL so I can access it via C#? Janene
0 Kudos
Zhang_Z_Intel
Employee
829 Views
The download links in this article have already been fixed: http://software.intel.com/en-us/articles/using-intel-mkl-in-your-c-program/ You can now download the examples in the article and learn how to use MKL in C#. These examples cover BLAS, LAPACK, FFT, and vector math. I do not have an example for using VSL in C# per se. But those examples should give you a pretty good idea about how to use MKL functions in C# in general. In addition, the following links will get you started with using the summary statistics functions in MKL: Thanks, Zhang
0 Kudos
Janene_Pappas-mccril
829 Views
[EDITED] I've used the examples to construct a test wrapper, but I'm having difficulty getting the task to calculate properly. A bit of assistance would be appreciated. I've attached my attempt. Thank you in advance, Janene
0 Kudos
Andrey_N_Intel
Employee
829 Views
Hi Janene, As a quick comment on your code: you use double precision version of the VSL SS task constructor and editors (which is specified by letter 'd' after 'vsl' prefix in the function names) VSL.vsldSSNewTask(ref task, ref dim, ref n, ref VSL.VSL_SS_MATRIX_STORAGE_ROWS, A, null, null); VSL.vsldSSEditTask(task, VSL.VSL_SS_ED_MIN, min_est); VSL.vsldSSEditTask(task, VSL.VSL_SS_ED_MAX, max_est); but use single precision version of the Compute function ( letter 's' after 'vsl' prefix): VSL.vslsSSCompute(task, VSL.VSL_SS_MIN | VSL.VSL_SS_MAX, VSL.VSL_SS_METHOD_FAST); I wonder if this could the reason of the wrong result? Thanks, Andrey
0 Kudos
Janene_Pappas-mccril
829 Views
Thanks Andrey. I've fixed this and it runs now, but the output is incorrect. The following code yields an RMS of 1, max of 25, min of 81 and mean of 1, all clearly incorrect. If someone knows what I've done incorrectly, I'd really appreciate some help. Janene [csharp] namespace mkl { public class vslTest { public static void Main(string[] args) { /* Data initialization - all parameters to MKL functions */ double[] vector = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int p = vector.Length; int n = 1; double[] mom = { 0 }; double[] max = { 0 }; double[] min = { 0 }; double[] mean = { 0 }; IntPtr task = new IntPtr(); int xstorage = VSL.VSL_SS_MATRIX_STORAGE_ROWS; /* setup task and compute */ // n vectors of dimension p VSL.vsldSSNewTask(ref task, ref p, ref n, ref xstorage, vector, null, null); VSL.vsldSSEditTask(task, VSL.VSL_SS_ED_2R_MOM, mom); VSL.vsldSSEditTask(task, VSL.VSL_SS_ED_MAX, max); VSL.vsldSSEditTask(task, VSL.VSL_SS_ED_MIN, min); VSL.vsldSSEditTask(task, VSL.VSL_SS_ED_MEAN, mean); VSL.vsldSSCompute(task, VSL.VSL_SS_2R_MOM|VSL.VSL_SS_MEAN | VSL.VSL_SS_MIN | VSL.VSL_SS_MAX, VSL.VSL_SS_METHOD_FAST); double rss = Math.Sqrt(mom[0]); double d = min[0] + max[0]; Console.WriteLine("D = " + d); Console.WriteLine("RMS = " + rss); Console.WriteLine("MIN = " + min[0]); Console.WriteLine("MAX = " + max[0]); Console.WriteLine("Mean = " + mean[0]); VSL.vslSSDeleteTask(ref task); // don't forget to delete when done } } /** VSL wrappers */ public sealed class VSL { // constants that are defined in mkl_vsl_defines.h public static int VSL_SS_MATRIX_STORAGE_ROWS = 0x00010000; public static int VSL_SS_MATRIX_STORAGE_COLS = 0x00020000; public const int VSL_SS_METHOD_FAST = 0x00000001; public const int VSL_SS_ED_MEAN = 7; public const int VSL_SS_ED_2R_MOM = 8; public const int VSL_SS_ED_MIN = 16; public const int VSL_SS_ED_MAX = 17; public const int VSL_SS_MEAN = 0x0000000000000001; public const int VSL_SS_2R_MOM = 0x0000000000000002; public const int VSL_SS_MIN = 0x0000000000000400; public const int VSL_SS_MAX = 0x0000000000000800; private VSL() { } /** VSL vsldSSNewTaskref wrapper */ public static void vsldSSNewTask(ref IntPtr taskPtr, ref int dim, ref int num, ref int xstorage, double[] x, double[] weights, int[] indices) { VSLNative.vsldSSNewTask(ref taskPtr, ref dim, ref num, ref xstorage, x, weights, indices); } /** VSL vslsSSEditTask wrapper */ public static void vsldSSEditTask(IntPtr taskPtr, int taskNum, double[] results) { VSLNative.vsldSSEditTask(taskPtr, taskNum, results); } /** VSL vsldSSCompute wrapper */ public static void vsldSSCompute(IntPtr taskPtr, int taskNum, int method) { VSLNative.vsldSSCompute(taskPtr, taskNum, method); } /** VSL vslSSDeleteTask wrapper */ public static void vslSSDeleteTask(ref IntPtr taskPtr) { VSLNative.vslSSDeleteTask(ref taskPtr); } } /** VSL native declarations */ [SuppressUnmanagedCodeSecurity] internal sealed class VSLNative { private VSLNative() { } /** VSL vsldSSNewTask native declaration */ [DllImport("mkl_rt", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)] internal static extern int vsldSSNewTask([In, Out] ref IntPtr task, [In] ref int p, [In] ref int n, [In] ref int xstorage, [In] double[] x, [In] double[] w, [In] int[] indices); /** VSL vsldSSEditTask native declaration */ [DllImport("mkl_rt", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)] internal static extern int vsldSSEditTask([In] IntPtr task, [In] int parameter, [Out] double[] par_addr); /** VSL vslsSSCompute native declaration */ [DllImport("mkl_rt", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)] internal static extern int vsldSSCompute([In] IntPtr task, [In] int estimates, [In] int method); /** VSL vslSSDeleteTask native declaration */ [DllImport("mkl_rt", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)] internal static extern int vslSSDeleteTask([In] ref IntPtr task); } }[/csharp]
0 Kudos
Andrey_N_Intel
Employee
830 Views
Hi Janene, Can you please clarify the structure of your dataset in the example? Which of the variants below is correct: 1. the dimension of your random vector is 1 and number of observations 9 or 2. the dimension of your random vector is 9 and number of observations is 1? If you assume the first case please set task dimension p to 1 and number of observations n to vector.Length. If you assume the second case (that is, you have just one observation of 9d vector) the estimates are expected to be of size 9, and you need to allocate arrays of size 9 for mean/min/max/mom (min and max arrays should be properly initialized) Another observation is that in the signature vsldSSCompute([In] IntPtr task, [In] int estimates, [In] int method); you use 32 bit integer for the parameter "estimates" while in Intel(R) MKL this parameter is 64-bit integer (this, however, is not expected to impact on the result in your example). Please, let me know which result you have after the fixes on your side. Thanks, Andrey
0 Kudos
Janene_Pappas-mccril
829 Views
Thank you so much Andrey, That fixed it all up. Janene
0 Kudos
Andrey_N_Intel
Employee
829 Views
Hi Janene, great to know you resolved the issues. Please, feel free to ask more questions on statistical component of Intel(R) MKL, and we'll help. Thanks, Andrey
0 Kudos
Reply