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

C# mkl_alloc mkl_dcscmv sparse matrix/vector multiply

aolney
Beginner
492 Views
I'm trying to usemkl_dcscmv with byte aligned storage. Right now the function returns but doesn't appear to do any work. If I pass in a non-zero result vector it emerges unchanged.

This is the test class

[csharp] public class mkl_dcscmv { private mkl_dcscmv() { } unsafe public static void Go(string[] args) { //create 4 arrays for storage of matrix UIntPtr valSize = new UIntPtr(6); int alignment = 32; double* valP = (double*) MKL.mkl_malloc(valSize, alignment).ToPointer(); int* rowIndexP = (int*)MKL.mkl_malloc(valSize, alignment).ToPointer(); UIntPtr indexSize = new System.UIntPtr(3); int* colbP = (int*) MKL.mkl_malloc(indexSize, alignment).ToPointer(); int* coleP = (int*) MKL.mkl_malloc(indexSize, alignment).ToPointer(); //create input array double* xP = (double*) MKL.mkl_malloc(indexSize, alignment).ToPointer(); //create result array double* resultP = (double*) MKL.mkl_malloc(indexSize, alignment).ToPointer(); //create matrix description char[] matdesc = new char[] { 'G', ' ', ' ', 'C' }; //general, zero based indexing //populate the arrays double[] val = new double[]{1,3,4,5,7,6}; int[] rowI = new int[]{1,2,0,2,1,2}; int[] colb = new int[]{0,2,4}; int[]cole = new int[]{2,4,6}; for (int i = 0; i < val.Length; i++) { *(valP + i) = val; *(rowIndexP + i) = rowI; Console.WriteLine(*(valP + i) + "\t" + *(rowIndexP + i)); } for (int i = 0; i < colb.Length; i++) { *(coleP + i) = cole; *(colbP + i) = colb; *(xP + i ) = 3; *(resultP + i) = 0; Console.WriteLine(*(coleP + i) + "\t" + *(colbP + i)); } int TransA = CBLAS.TRANSPOSE.NoTrans; int rows = 3; int cols = 3; double alpha = 2.0, beta = 1.0; CBLAS.mkl_dcscmv(ref TransA, ref rows, ref cols, ref alpha, matdesc, valP, rowIndexP, colbP, coleP, xP, ref beta, resultP); //CBLAS.mkl_dcscmv( TransA, rows, cols, alpha, matdesc, valP, rowIndexP, colbP, coleP, xP, beta, resultP); for (int i = 0; i < rows; i++) Console.WriteLine(*(resultP + i)); } }[/csharp]
Now the support/wrapper classes:
[csharp]namespace mkl { /** CBLAS wrappers */ unsafe public sealed class CBLAS { private CBLAS() {} /** Constants for CBLAS_ORDER enum, file "mkl_cblas.h" */ public sealed class ORDER { private ORDER() {} public static int RowMajor=101; /* row-major arrays */ public static int ColMajor=102; /* column-major arrays */ } /** Constants for CBLAS_TRANSPOSE enum, file "mkl_cblas.h" */ public sealed class TRANSPOSE { private TRANSPOSE() {} public static int NoTrans =111; /* trans='N' */ public static int Trans =112; /* trans='T' */ public static int ConjTrans=113; /* trans='C' */ } unsafe public static void mkl_dcscmv(ref int TransA, ref int rows, ref int cols, ref double alpha, char[] matdescra, double* values, int* rowIndex, int* colb, int* cole, double* x, ref double beta, double* y) { CBLASNative.mkl_dcscmv(ref TransA, ref rows, ref cols, ref alpha, matdescra, values, rowIndex, colb, cole, x, ref beta, y); } } /** CBLAS native declarations */ [SuppressUnmanagedCodeSecurity] unsafe internal sealed class CBLASNative { private CBLASNative() {} [DllImport("mkl_rt", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)] internal static extern void mkl_dcscmv( ref int TransA, ref int rows, ref int cols, ref double alpha, [In] char[] matdescra, [In] double* values, [In] int* rowIndex, [In] int* colb, [In] int* cole, [In] double* x, ref double beta, [In, Out] double* y); } public sealed class MKL { public static UIntPtr mkl_malloc(UIntPtr size, int alignment) { return MKLNative.MKL_malloc(size, alignment); } public static void mkl_free(UIntPtr ptr) { MKLNative.MKL_free(ptr); } } [SuppressUnmanagedCodeSecurity] internal sealed class MKLNative { [DllImport("mkl_rt", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)] internal static extern UIntPtr MKL_malloc(UIntPtr size, int alignment); [DllImport("mkl_rt", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)] internal static extern void MKL_free(UIntPtr ptr); } }[/csharp]
0 Kudos
2 Replies
Gennady_F_Intel
Moderator
492 Views
Did you check the plane "C" code with mkl_dcscmv?
compiling and executing your exampe, I got:
1 1
3 2
4 0
5 2
7 1
6 2
2 0
4 2
6 4
MKL ERROR: Parameter 1 was incorrect on entry to MKL_DCSCMV
0
0
0 Kudos
Ying_H_Intel
Employee
492 Views
Hi

the cblas sample in http://software.intel.com/en-us/articles/using-intel-mkl-in-your-c-program/mainly for c interface, so have a cblas wraper. It works fine when call cblas function. but tt seems bring a little pinprick when you call fortran interface function mkl_dcscmv().

The cblas wrapper is not needed actually, for example, the below code, you will get right result.
http://software.intel.com/en-us/forums/showthread.php?t=85690

Best Regards,
Ying

using System;
using System.Security;
using System.Runtime.InteropServices;
using mkl;


public class mkl_dcscmv
{
private mkl_dcscmv() { }
unsafe public static void Main(string[] args)
{
//create 4 arrays for storage of matrix
UIntPtr valSize = new UIntPtr(6);
int alignment = 32;

double* valP = (double*)MKL.mkl_malloc(valSize, alignment).ToPointer();
int* rowIndexP = (int*)MKL.mkl_malloc(valSize, alignment).ToPointer();

UIntPtr indexSize = new System.UIntPtr(3);

int* colbP = (int*)MKL.mkl_malloc(indexSize, alignment).ToPointer();
int* coleP = (int*)MKL.mkl_malloc(indexSize, alignment).ToPointer();

//create input array
double* xP = (double*)MKL.mkl_malloc(indexSize, alignment).ToPointer();

//create result array
double* resultP = (double*)MKL.mkl_malloc(indexSize, alignment).ToPointer();

//create matrix description
char[] matdesc = new char[] { 'G', ' ', ' ', 'C' }; //general, zero based indexing

//populate the arrays
double[] val = new double[] { 1, 3, 4, 5, 7, 6 };
int[] rowI = new int[] { 1, 2, 0, 2, 1, 2 };
int[] colb = new int[] { 0, 2, 4 };
int[] cole = new int[] { 2, 4, 6 };

for (int i = 0; i < val.Length; i++)
{
*(valP + i) = val;
*(rowIndexP + i) = rowI;

Console.WriteLine(*(valP + i) + "\t" + *(rowIndexP + i));
}

for (int i = 0; i < colb.Length; i++)
{
*(coleP + i) = cole;
*(colbP + i) = colb;
*(xP + i) = 3;
*(resultP + i) = 0;
Console.WriteLine(*(coleP + i) + "\t" + *(colbP + i));
}

int TransA = 'N'; // CBLAS.TRANSPOSE.NoTrans;
int rows = 3;
int cols = 3;
double alpha = 2.0, beta = 1.0;
CBLASNative.mkl_dcscmv(ref TransA, ref rows, ref cols, ref alpha, matdesc, valP, rowIndexP, colbP, coleP, xP, ref beta, resultP);
//CBLAS.mkl_dcscmv( TransA, rows, cols, alpha, matdesc, valP, rowIndexP, colbP, coleP, xP, beta, resultP);

for (int i = 0; i < rows; i++)
Console.WriteLine(*(resultP + i));
}

}


namespace mkl
{
//remove the wrapper;
/** CBLAS native declarations */
[SuppressUnmanagedCodeSecurity]
unsafe internal sealed class CBLASNative
{
private CBLASNative() {}

[DllImport("mkl_rt", CallingConvention = CallingConvention.Cdecl,
ExactSpelling = true, SetLastError = false)]
internal static extern void mkl_dcscmv(
ref int TransA, ref int rows, ref int cols, ref double alpha,
[In] char[] matdescra, [In] double* values,
[In] int* rowIndex, [In] int* colb,
[In] int* cole, [In] double* x,
ref double beta, [In, Out] double* y);

}
public sealed class MKL
{
public static UIntPtr mkl_malloc(UIntPtr size, int alignment)
{
return MKLNative.MKL_malloc(size, alignment);
}
public static void mkl_free(UIntPtr ptr)
{
MKLNative.MKL_free(ptr);
}
}

[SuppressUnmanagedCodeSecurity]
internal sealed class MKLNative
{
[DllImport("mkl_rt", CallingConvention = CallingConvention.Cdecl,
ExactSpelling = true, SetLastError = false)]
internal static extern UIntPtr MKL_malloc(UIntPtr size, int alignment);
[DllImport("mkl_rt", CallingConvention = CallingConvention.Cdecl,
ExactSpelling = true, SetLastError = false)]
internal static extern void MKL_free(UIntPtr ptr);
}
}

build and run it:
1 1
3 2
4 0
5 2
7 1
6 2
2 0
4 2
6 4
24
48
84
.... . .

0 Kudos
Reply