2008-07-31 11:10:29 +02:00
|
|
|
/*
|
2009-03-11 15:57:34 +01:00
|
|
|
* Copyright (C) 2007-2009 Dynare Team
|
2008-07-31 11:10:29 +02:00
|
|
|
*
|
|
|
|
* This file is part of Dynare.
|
|
|
|
*
|
|
|
|
* Dynare is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Dynare is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This mex file computes A*kron(B,C) or A*kron(B,B) without explicitely building kron(B,C) or kron(B,B), so that
|
|
|
|
* one can consider large matrices B and/or C.
|
|
|
|
*/
|
|
|
|
|
2007-12-19 16:58:38 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include "mex.h"
|
2009-04-09 15:36:06 +02:00
|
|
|
#include "../matlab_versions_compatibility.h"
|
2007-12-19 16:58:38 +01:00
|
|
|
|
2009-03-14 23:51:50 +01:00
|
|
|
#ifdef NO_BLAS_H
|
|
|
|
#if defined(__linux__) || defined(OCTAVE)
|
|
|
|
#define dgemm dgemm_
|
|
|
|
#endif
|
|
|
|
extern "C" {
|
2009-04-09 15:36:06 +02:00
|
|
|
lapack_int dgemm(char*, char*, lapack_int*, lapack_int*, lapack_int*, double*, double*, lapack_int*, double*, lapack_int*, double*, double*, lapack_int*);
|
2009-03-14 23:51:50 +01:00
|
|
|
}
|
|
|
|
#else /* NO_BLAS_H */
|
|
|
|
#include "blas.h"
|
|
|
|
#endif /* NO_BLAS_H */
|
2009-09-18 12:34:04 +02:00
|
|
|
|
|
|
|
#ifdef USE_OMP
|
2009-04-03 13:01:55 +02:00
|
|
|
#include <omp.h>
|
2009-03-14 23:51:50 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MWTYPES_NOT_DEFINED
|
|
|
|
typedef int mwIndex;
|
|
|
|
typedef int mwSize;
|
2009-03-11 15:57:34 +01:00
|
|
|
#endif
|
|
|
|
|
2007-12-19 16:58:38 +01:00
|
|
|
void full_A_times_kronecker_B_C(double *A, double *B, double *C, double *D,
|
2009-04-09 15:36:06 +02:00
|
|
|
lapack_int mA, lapack_int nA, lapack_int mB, lapack_int nB, lapack_int mC, lapack_int nC)
|
2007-12-19 16:58:38 +01:00
|
|
|
{
|
2009-03-11 15:57:34 +01:00
|
|
|
#if USE_OMP
|
|
|
|
#pragma omp parallel for num_threads(atoi(getenv("DYNARE_NUM_THREADS")))
|
2009-03-14 23:35:44 +01:00
|
|
|
for(long int colD = 0; colD<nB*nC; colD++)
|
|
|
|
{
|
|
|
|
#if DEBUG_OMP
|
|
|
|
mexPrintf("%d thread number is %d (%d).\n",colD,omp_get_thread_num(),omp_get_num_threads());
|
|
|
|
#endif
|
|
|
|
unsigned int colB = colD/nC;
|
|
|
|
unsigned int colC = colD%nC;
|
|
|
|
for(int colA = 0; colA<nA; colA++)
|
|
|
|
{
|
|
|
|
unsigned int rowB = colA/mC;
|
|
|
|
unsigned int rowC = colA%mC;
|
|
|
|
unsigned int idxA = colA*mA;
|
|
|
|
unsigned int idxD = colD*mA;
|
|
|
|
double BC = B[colB*mB+rowB]*C[colC*mC+rowC];
|
|
|
|
for(int rowD = 0; rowD<mA; rowD++)
|
|
|
|
{
|
|
|
|
D[idxD+rowD] += A[idxA+rowD]*BC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
const unsigned long shiftA = mA*mC ;
|
|
|
|
const unsigned long shiftD = mA*nC ;
|
|
|
|
unsigned long int kd = 0, ka = 0 ;
|
|
|
|
char transpose[2] = "N";
|
|
|
|
double one = 1.0 ;
|
|
|
|
for(unsigned long int col=0; col<nB; col++)
|
|
|
|
{
|
|
|
|
ka = 0 ;
|
|
|
|
for(unsigned long int row=0; row<mB; row++)
|
|
|
|
{
|
|
|
|
dgemm(transpose, transpose, &mA, &nC, &mC, &B[mB*col+row], &A[ka], &mA, &C[0], &mC, &one, &D[kd], &mA);
|
|
|
|
ka += shiftA;
|
|
|
|
}
|
|
|
|
kd += shiftD;
|
|
|
|
}
|
2009-03-11 15:57:34 +01:00
|
|
|
#endif
|
2007-12-19 16:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-18 16:19:05 +02:00
|
|
|
void full_A_times_kronecker_B_B(double *A, double *B, double *D, lapack_int mA, lapack_int nA, lapack_int mB, lapack_int nB)
|
2007-12-19 16:58:38 +01:00
|
|
|
{
|
2009-03-11 15:57:34 +01:00
|
|
|
#if USE_OMP
|
|
|
|
#pragma omp parallel for num_threads(atoi(getenv("DYNARE_NUM_THREADS")))
|
2009-03-14 23:35:44 +01:00
|
|
|
for(long int colD = 0; colD<nB*nB; colD++)
|
|
|
|
{
|
|
|
|
#if DEBUG_OMP
|
|
|
|
mexPrintf("%d thread number is %d (%d).\n",colD,omp_get_thread_num(),omp_get_num_threads());
|
|
|
|
#endif
|
|
|
|
unsigned int j1B = colD/nB;
|
|
|
|
unsigned int j2B = colD%nB;
|
|
|
|
for(int colA = 0; colA<nA; colA++)
|
|
|
|
{
|
|
|
|
unsigned int i1B = colA/mB;
|
|
|
|
unsigned int i2B = colA%mB;
|
|
|
|
unsigned int idxA = colA*mA;
|
|
|
|
unsigned int idxD = colD*mA;
|
|
|
|
double BB = B[j1B*mB+i1B]*B[j2B*mB+i2B];
|
|
|
|
for(int rowD = 0; rowD<mA; rowD++)
|
|
|
|
{
|
|
|
|
D[idxD+rowD] += A[idxA+rowD]*BB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
const unsigned long int shiftA = mA*mB ;
|
|
|
|
const unsigned long int shiftD = mA*nB ;
|
|
|
|
unsigned long int kd = 0, ka = 0 ;
|
|
|
|
char transpose[2] = "N";
|
|
|
|
double one = 1.0;
|
|
|
|
for(unsigned long int col=0; col<nB; col++)
|
|
|
|
{
|
|
|
|
ka = 0;
|
|
|
|
for(unsigned long int row=0; row<mB; row++)
|
|
|
|
{
|
|
|
|
dgemm(transpose, transpose, &mA, &nB, &mB, &B[mB*col+row], &A[ka], &mA, &B[0], &mB, &one, &D[kd], &mA);
|
|
|
|
ka += shiftA;
|
|
|
|
}
|
|
|
|
kd += shiftD;
|
|
|
|
}
|
|
|
|
#endif
|
2007-12-19 16:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
|
|
|
|
{
|
|
|
|
// Check input and output:
|
|
|
|
if ( (nrhs > 3) || (nrhs <2) )
|
|
|
|
{
|
|
|
|
mexErrMsgTxt("Two or Three input arguments required.");
|
|
|
|
}
|
|
|
|
if (nlhs>1)
|
|
|
|
{
|
|
|
|
mexErrMsgTxt("Too many output arguments.");
|
|
|
|
}
|
|
|
|
// Get & Check dimensions (columns and rows):
|
|
|
|
mwSize mA, nA, mB, nB, mC, nC;
|
|
|
|
mA = mxGetM(prhs[0]);
|
|
|
|
nA = mxGetN(prhs[0]);
|
|
|
|
mB = mxGetM(prhs[1]);
|
|
|
|
nB = mxGetN(prhs[1]);
|
|
|
|
if (nrhs == 3)// A*kron(B,C) is to be computed.
|
|
|
|
{
|
|
|
|
mC = mxGetM(prhs[2]);
|
|
|
|
nC = mxGetN(prhs[2]);
|
|
|
|
if (mB*mC != nA)
|
|
|
|
{
|
|
|
|
mexErrMsgTxt("Input dimension error!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else// A*kron(B,B) is to be computed.
|
|
|
|
{
|
|
|
|
if (mB*mB != nA)
|
|
|
|
{
|
|
|
|
mexErrMsgTxt("Input dimension error!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Get input matrices:
|
|
|
|
double *B, *C, *A;
|
|
|
|
A = mxGetPr(prhs[0]);
|
|
|
|
B = mxGetPr(prhs[1]);
|
|
|
|
if (nrhs == 3)
|
|
|
|
{
|
|
|
|
C = mxGetPr(prhs[2]);
|
|
|
|
}
|
|
|
|
// Initialization of the ouput:
|
|
|
|
double *D;
|
|
|
|
if (nrhs == 3)
|
|
|
|
{
|
|
|
|
plhs[0] = mxCreateDoubleMatrix(mA,nB*nC,mxREAL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
plhs[0] = mxCreateDoubleMatrix(mA,nB*nB,mxREAL);
|
|
|
|
}
|
|
|
|
D = mxGetPr(plhs[0]);
|
|
|
|
// Computational part:
|
|
|
|
if (nrhs == 2)
|
|
|
|
{
|
2008-06-23 11:38:46 +02:00
|
|
|
full_A_times_kronecker_B_B(A, B, &D[0], (int) mA, (int) nA, (int) mB, (int) nB);
|
2007-12-19 16:58:38 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-06-23 11:38:46 +02:00
|
|
|
full_A_times_kronecker_B_C(A, B, C, &D[0], (int) mA, (int) nA, (int) mB, (int) nB, (int) mC, (int) nC);
|
2007-12-19 16:58:38 +01:00
|
|
|
}
|
|
|
|
}
|