This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Potential memory problem relating to printf and malloc on DSK 6713

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_*/


 

  • Jack,

    I did not analyze your code thoroughly, but no problems starred me in the face.

    Therefore, I will start with your last paragraph: it is strongly recommend to check the vector and matrix math functions of the C67x DSPLIB - check this wiki page.

    Jack Harris1 said:

    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).

    malloc() returning NULL means it couldn't allocate the data - your configurations seem to indicate you have enough heap for whatever you are doing, therefore I wonder if you are allocating this to invalid memory somehow. If this seems to be your case, the linker CMD file is responsible for placing code/data to the device memory - check section 3.2.2 of this page for additional details. Another detail is that, if the linker CMD file is allocating this to external memory, you must pre-configure the EMIF peripheral using a GEL script to properly set this when connecting your board (Spectrum Digital has one here). Since the C6713 device has plenty of internal memory for this code (256kB if I recall correctly), I would recommend adjusting your linker CMD file to allocate everything to internal memory at this time - some information about that can be obtained here and here.

    The other day I was helping another customer with a 2D array allocation routine and I created a small project with code. Check attached the adapted version for the C6713 (it contains a project for CCSv5.5, but the source code and the linker CMD file are compatible).

    One detail that is not very relevant for this particular case but may save you from trouble as you progress with your design is to update your copy of CCS: CCSv4.0 is at least four years old and it is unsupported (no bug fixes). You can download CCSv5.5, install it to a different directory and use a different workspace - this way you don't disrupt your current development environment.

    Hope this helps,

    Rafael

     

    C6713_2DArray.zip
  • Hi Rafael,

    Thanks so much for the reply.

    1) I switched to using the C67x DSPLIB library and it's saved loads of time - thanks.

    2) Despite changing the stack size to 0x8000 (under the basic linker options), when I look at the .cmd file :

    /* MODULE MEM */
    -stack 0x400
    MEMORY {
    SDRAM : origin = 0x80000000, len = 0x800000
    IRAM : origin = 0x0, len = 0x40000
    }

    So this doesn't seem to have an affect on the cmd file.I believe that I am using DSP/BIOS. See image below.

    3) My problem seems to be related to the problem here: http://e2e.ti.com/support/development_tools/code_composer_studio/f/81/t/211186.aspx, but I can't seem to get a report of how my memory is used. I've tried to use the method under: How to diagnose (Method 1) in http://processors.wiki.ti.com/index.php/DSP_BIOS_Debugging_Tips#Cause:_Stack_overflow, but when I click on Tools > ROV, I get an error in the window that appears:

    "ROV failed to start. If the problem was with the XDCtools root or path, terminate and restart the debug session before trying again.

    Unsupported ISA 103". 

    I've restarted the debug session several times.

    4) I've written a function that performs a short time Fourier transform (I'd love to know if there's a library function that exists because I am not sure how reliable mine is). But it hangs on a cosine function built into C, which could suggest a stack overflow, hence why I was looking at the page that I linked to above.

    Thanks,
    Jack 

  • It's been a while and no one's replied.

    It's now obvious that my program is in DSP/BIOS mode.

    But I still don't know why my program would hang on a cos (cosine) function in DSP/BIOS mode, is this due to a stack overflow?

    Thanks,

    Jack