LAMA
/home/brandes/workspace/LAMA/src/lama/openmp/SCALAPACKHelper.hpp
Go to the documentation of this file.
00001 
00033 #ifndef LAMA_SCALAPACKHELPER_HPP_
00034 #define LAMA_SCALAPACKHELPER_HPP_
00035 
00036 // macros
00037 #include <lama/macros/unused.hpp>
00038 
00039 #include <mkl_scalapack.h>
00040 #include <mkl_blacs.h>
00041 
00042 #ifndef WIN32
00043 #define F77_descinit        descinit_
00044 #define F77_numroc          numroc_
00045 #define F77_blacs_get       blacs_get_
00046 #define F77_blacs_gridinit  blacs_gridinit_
00047 #define F77_blacs_gridinfo  blacs_gridinfo_
00048 #define F77_blacs_gridexit  blacs_gridexit_
00049 #define F77_blacs_freebuff  blacs_freebuff_
00050 #else
00051 #define F77_descinit        DESCINIT
00052 #define F77_numroc          NUMROC
00053 #define F77_blacs_get       BLACS_GET
00054 #define F77_blacs_gridinit  BLACS_GRIDINIT
00055 #define F77_blacs_gridinfo  BLACS_GRIDINFO
00056 #define F77_blacs_gridexit  BLACS_GRIDEXIT
00057 #define F77_blacs_freebuff  BLACS_FREEBUFF
00058 #endif
00059 
00060 
00061 #ifdef __cplusplus
00062 extern "C" {
00063 #endif
00064 
00065 extern void F77_descinit( int*, int*, int*, int*, int*, int*, int*, int*, int*, int* );
00066 extern int F77_numroc( int*, int*, int*, int*, int*);
00067 
00068 #ifdef __cplusplus
00069 }
00070 #endif
00071 
00072 struct SCALAPACKHelper
00073 {
00074 public:
00075 
00076     static void BLACS_Context( int* context );
00077 
00078     static void GridInit( int* context, const char* kind, const int npRow, const int npCol );
00079 
00080     static void GridInfo( const int Context, const int npRow, const int npCol, int* myRow, int* myCol );
00081 
00082     static void GridExit( const int context );
00083 
00084     static void BlacsFreeBuf( const int context );
00085 
00086     static int DescInit( int* descA, int m, int n, int mb, int nb, int irscr, int icsrc, int ictxt, int lld);
00087 
00088     static int numRoC( int n, int nb, int iproc, int nprocs );
00089 
00095     static int ILCM(int m, int n);
00096 
00097 };
00098 
00099 #ifdef LAMA_MKL_SCALAPACK
00100 void SCALAPACKHelper::BLACS_Context( int* context )
00101 {
00102     MKL_INT what = 0;   // ask for system context
00103     MKL_INT val = -1;
00104     F77_blacs_get( &val, &what, context );
00105 }
00106 #else
00107 void SCALAPACKHelper::BLACS_Context( int* UNUSED(context) )
00108 {
00109     // ERROR
00110     printf( "ERROR: BLACS not available\n" );
00111 }
00112 #endif
00113 
00114 
00115 #ifdef LAMA_MKL_SCALAPACK
00116 void SCALAPACKHelper::GridInit( int* context, const char* kind, const int npRow, const int npCol )
00117 {
00118     F77_blacs_gridinit( context, (char*) kind, (int*) &npRow, (int*) &npCol );
00119 }
00120 #else
00121 void SCALAPACKHelper::GridInit( int* UNUSED(context), const char* UNUSED(kind), const int UNUSED(npRow),
00122     const int UNUSED(npCol) )
00123 {}
00124 #endif
00125 
00126 
00127 #ifdef LAMA_MKL_SCALAPACK
00128 void SCALAPACKHelper::GridInfo( const int Context, const int npRow, const int npCol, int* myRow, int* myCol )
00129 {
00130     F77_blacs_gridinfo( (int*) &Context, (int*) &npRow, (int*) &npCol, myRow, myCol );
00131 }
00132 #else
00133 void SCALAPACKHelper::GridInfo(
00134     const int UNUSED(Context),
00135     const int UNUSED(npRow),
00136     const int UNUSED(npCol),
00137     int* UNUSED(myRow),
00138     int* UNUSED(myCol) )
00139 {}
00140 #endif
00141 
00142 
00143 #ifdef LAMA_MKL_SCALAPACK
00144 void SCALAPACKHelper::GridExit( const int context )
00145 {
00146     F77_blacs_gridexit( (int*)( &context ) );
00147 }
00148 #else
00149 void SCALAPACKHelper::GridExit( const int UNUSED(context) )
00150 {}
00151 #endif
00152 
00153 #ifdef LAMA_MKL_SCALAPACK
00154 void SCALAPACKHelper::BlacsFreeBuf( const int context )
00155 {
00156     MKL_INT i_one = 1;
00157     F77_blacs_freebuff( (int*)( &context ), &i_one );
00158 }
00159 #else
00160 void SCALAPACKHelper::BlacsFreeBuf( const int UNUSED(context) )
00161 {}
00162 #endif
00163 
00164 
00165 #ifdef LAMA_MKL_SCALAPACK
00166 int SCALAPACKHelper::DescInit( int* descA, int m, int n, int mb, int nb, int irscr, int icsrc, int ictxt, int lld)
00167 {
00168     int info = 0;
00169     F77_descinit( descA, &m, &n, &mb, &nb, &irscr, &icsrc, &ictxt, &lld, &info );
00170     return info;
00171 }
00172 #else
00173 int SCALAPACKHelper::DescInit(
00174     int* UNUSED(descA),
00175     int UNUSED(m),
00176     int UNUSED(n),
00177     int UNUSED(mb),
00178     int UNUSED(nb),
00179     int UNUSED(irscr),
00180     int UNUSED(icsrc),
00181     int UNUSED(ictxt),
00182     int UNUSED(lld) )
00183 {
00184     return 0;
00185 }
00186 #endif
00187 
00188 
00189 #ifdef LAMA_MKL_SCALAPACK
00190 int SCALAPACKHelper::numRoC( int n, int nb, int iproc, int nprocs )
00191 {
00192     int iSrcProc = 0; // first row/col always on first processor
00193     return F77_numroc (&n, &nb, &iproc, &iSrcProc, &nprocs );
00194 }
00195 #else
00196 int SCALAPACKHelper::numRoC( int UNUSED(n), int UNUSED(nb), int UNUSED(iproc), int UNUSED(nprocs) )
00197 {
00198     return -1;
00199 }
00200 #endif
00201 
00202 
00203 int SCALAPACKHelper::ILCM(int m, int n)
00204 {
00205     int ia,iq,ir;
00206     int ilcm;
00207 
00208     if (m >= n)
00209     {
00210         ia = m;
00211         ilcm = n;
00212     }
00213     else
00214     {
00215         ia = n;
00216         ilcm = m;
00217     }
00218 
00219     iq = ia / ilcm;
00220     ir = ia - iq * ilcm;
00221     while (ir != 0)
00222     {
00223         ia = ilcm;
00224         ilcm = ir;
00225         iq = ia / ilcm;
00226         ir = ia - iq * ilcm;
00227     }
00228     return ( m * n ) / ilcm;
00229 }
00230 
00231 #endif // LAMA_SCALAPACKHELPER_HPP_