Hello all,
I currently trying to write some code in code composer studio (Version: 4.0.1.01001) which does a matrix multiplication on a DSK 6713 DSP board.
I've written a function called matmat()which does this, beforehand I allocate the memory for the two matrices and the result using malloc, please see the code below. Eventually I want to be able to multiply a 2x2 matrix by a 2 x many matrix, I am using two 2x2 matrices at the moment to get my code working.
My question is; when I call malloc why does it return 0x000000 ? (so that two matrices are stored in the same place in memory, which obviously leads to an incorrect calculation). In an attempt to solve the problem of printf not working (which seems to be well documented), I've set the heap and stack sizes so that they're both 0x800, then 0x8000 (which I understand is rather large?), but neither of these work (however printf does work if I set it to buffer one character at a time, but only for the first few calls to printf).
There seems to be a fundamental problem with the way the memory is being allocated (as malloc seems to be unable to allocate memory), but I am at a loss to what it could be, I've tried compiling the project on another PC then running it on another board and I get the same problem.
Any help would be greatly appreciated, if necessary I can supply further details.
Many thanks,
Jack
(ps. see below for code).
(pss. If there was already a library suitable for matrix multiplication, this could potentially solve my problem [at the very least it would give me something else to try], advice would be very much appreciated).
//----------------------------------------main.c-------------------------------------------
#define CHIP_6713 1
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "dsk6713.h" // Header files for the board
#include "dsk6713_aic23.h" // Header files for the codec n the board
#include "fft.h" // includes complex number definition
//#include "sigs.h"
#include "dotp.h"
#define S1_LEN 512
#define PI 3.14159265358979
#define N 1024
#define TESTFREQ 800.0
#define SAMPLING_FREQ 8000.0
#define LOOPLENGTH 8
#define WIN_LEN 32
void main()
{
int n;// Loop index
char *printf_buf; // printf buffer
float x[] ={3.0,1.0,2.3};
float y[] ={3.1,-1.2,2.1};
float **vv,**ss,**ww,**res,v[2][2] = {
{2.0,1.1},
{0.2,0.9}
};
float w[2][2] = {
{0.682,0.459},
{0.571,0.901}
};
DSK6713_init();
// Printf stuff
// printf_buf = (char *)malloc(BUFSIZ*sizeof(char));// This was a previous attempt at avoiding dynamically allocating the printf buffer
// setvbuf(stdout, printf_buf, _IONBF, BUFSIZ); // _IONBF sets it to no buffering
// setvbuf(stdout, NULL, _IONBF, BUFSIZ);
fflush(stdout); // This will flush any pending printf output
fprintf(stdout,"Jack's code\n"); // This only works if I buffer one character at a time
// Code to test vector/matrix multiplication
// dotp(x,y,3); // Dot product test - works.
// ss = (float**) malloc((unsigned) S1_LEN*sizeof(float*));
// for(n=0;n<S1_LEN;n++) ss[n]=s[n];
ww = (float**) malloc((unsigned) 2*sizeof(float*));
for(n=0;n<S1_LEN;n++) ww[n]=w[n];
vv = (float**) malloc((unsigned) 2*sizeof(float*));
for(n=0;n<S1_LEN;n++) vv[n]=v[n];
// vecTmat(ss,w,res,2,S1_LEN);
// matmat(ww, vv, res, 1, 2, 2, S1_LEN);
matmat(ww, vv, res, 2, 2, 2, 2);
///////////////////
fflush(stdout);
printf("Done!\n");// This has never worked even if I buffer one character at a time
}
//---------------------------------------dotp.h--------------------------------------------
#ifndef DOTP_
#define DOTP_
float dotp(float *a, float *b, int N)
{
float out;
int n;
for(n=0;n<N;n++)
{
out += (*a) * (*b);
a++;
b++;
}
return out;
}
// Multiplies a matrix by a vector
void matvec(float **a, float *b, float *out, int M, int N)
{
// float *out=NULL;//Need to use malloc for this
int m,n;
for(m=0; m<M; m++) *(out+m)=0;
for(m=0; m<M; m++)
{
for(n=0; n<N; n++)
{
*(out+m) += (a[m][n] * b[n]);
}
}
}
// Multiplies a vector (transposed) by a matrix
void vecTmat(float **a, float *b, float *out, int M, int N)
{
// float *out=NULL;//Need to use malloc for this
int m,n;
for(n=0; n<N; n++) *(out+n)=0;
for(n=0; n<N; n++)
{
for(m=0; m<M; m++)
{
*(out+n) += (b[m] * a[m][n]);
}
}
}
void matmat(float **a, float **b, float **out, int Ma, int Na, int Mb, int Nb)
{
int m,n,inner;
for(m=0; m<Ma; m++)
{
for(n=0; n<Nb; n++)
{
out[m][n]=0;
}
}
if (Na==Mb) // checks to see if the inner dimension is the same
{
for(m = 0; m < Ma; m++) // Go through the rows of a
{
for (n = 0; n < Nb; n++) // Go through the columns of b
{
// Multiply the row of A by the column of B to get the row, column of product.
for (inner = 0; inner < Na; inner++)
{
out[m][n] += a[m][inner] * b[inner][n];
}
}
}
}
}
#endif /*DOTP_*/
