Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8537633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:56:50+00:00 2026-06-11T10:56:50+00:00

I have a Mex-function (a function in c++ that you can call from Matlab)

  • 0

I have a Mex-function (a function in c++ that you can call from Matlab) that I have written, and I want to profile it using valgrind/kcachegrind. I know how to use valgrind/kcachegrind if you are running a c++ program directly, but is there a way to do this profiling if I am calling the c++ program from Matlab?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:56:51+00:00Added an answer on June 11, 2026 at 10:56 am

    Profiling MEX files is tricky since the MEX files are shared libraries. It can not be done on Linux using standard ‘gprof’ approach – gprof simply does not do that. I tried to use sprof, but I get “PLTREL not found error” – sprof can not be used either. There is a previous post here, but no one gave a final answer.

    Luckily, there is a way in which one can do it with valgrind on Linux. First, we need to write ‘running’ code that loads the mex file, provides the mexFunction symbol for us to call, and sets up the parameters of the MEX file. I have chosen to use the recommended way to do this with MATLAB – using MATLAB engine. The following code (save as test.c) loads a MEX file and finds the mexFunction symbol, loads input data from a file previously saved as ‘input.mat’ (can be done in MATLAB using save command), and calls the mexFunction.

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <dlfcn.h>
    #include "engine.h"
    
    typedef void (*mexFunction_t)(int nargout, mxArray *pargout [ ], int nargin, const mxArray *pargin[]);
    
    int main(int argc, const char *argv[])
    
    {
      Engine *ep;
      char buff[1024];
      int i;
    
      /* matlab must be in the PATH! */
      if (!(ep = engOpen("matlab -nodisplay"))) {
        fprintf(stderr, "Can't start MATLAB engine\n");
        return -1;
      }
      engOutputBuffer(ep, buff, 1023);
    
      /* load the mex file */
      if(argc<2){
        fprintf(stderr, "Error. Give full path to the MEX file as input parameter.\n");
        return -1;
      }
      void *handle = dlopen(argv[1], RTLD_NOW);
      if(!handle){
        fprintf(stderr, "Error loading MEX file: %s\n", strerror(errno));
        return -1;
      }
    
      /* grab mexFunction handle */
      mexFunction_t mexfunction = (mexFunction_t)dlsym(handle, "mexFunction");
      if(!mexfunction){
        fprintf(stderr, "MEX file does not contain mexFunction\n");
        return -1;
      }
    
      /* load input data - for convenience do that using MATLAB engine */
      /* NOTE: parameters are MEX-file specific, so one has to modify this*/
      /* to fit particular needs */
      engEvalString(ep, "load input.mat");
      mxArray *arg1 = engGetVariable(ep, "Ain");
      mxArray *arg2 = engGetVariable(ep, "opts");
      mxArray *pargout[1] = {0};
      const mxArray *pargin[2] = {arg1, arg2};
    
      /* execute the mex function */
      mexfunction(1, pargout, 2, pargin);
    
      /* print the results using MATLAB engine */
      engPutVariable(ep, "result", pargout[0]);
      engEvalString(ep, "result");
      printf("%s\n", buff);
    
      /* cleanup */
      mxDestroyArray(pargout[0]);
      engEvalString(ep, "clear all;");
      dlclose(handle);
      engClose(ep);
    
      return 0;
    }
    

    The MEX file itself should also compiled with the mex -g switch. The above code has to be compiled with mex -g and using engopts.sh as compilation parameters. From MATLAB command line type

    mex('-v', '-f', fullfile(matlabroot,...
        'bin','engopts.sh'),...
        'test.c');
    

    or in a standard Linux terminal run

    /path/to/matlab/bin/mex -g -f /path/to/matlab/bin/engopts.sh test.c
    

    Profiling the MEX file with valgrind requires running the ‘test’ program from the command line. In the directory where both test and the MEX file reside type the command:

    PATH=$PATH:/path/to/matlab/bin/ LD_LIBRARY_PATH=/path/to/matlab/bin/glnxa64/:/path/to/matlab/sys/os/glnxa64/ valgrind --tool=callgrind ./test ./mex_file.mexa64
    

    Note that the path to MATLAB and correct architecture-dependent library paths need to be set! matlab executable must be present in the PATH, otherwise ‘test’ will fail.

    There is one more catch. MATLAB engine requires csh to be installed on the system (you can use any shell, csh just needs to be present in /bin). So if you don’t have it, you have to install it for this to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a Matlab GUI for my C program. I thought about using
I have a mex function that takes a double precision input vector, downcasts the
I have an embedded Matlab function that itself calls other embedded Matlab functions and
I am currently writing a MEX function that will have to work with a
I have an existing web service and I want it to expose 2 MEX
I have a mex file (compiled in VS2010, Matlab 2010b) which accepts a variable,
I have a C++ file that: starts the matlab engine calls matlab_optimize() (a compiled
I have a double 2D image that I want to use in my UI
I am trying to create a mex file that interfaces MATLAB with an external
I'm currently writing a MEX file in MATLAB that uses the CPLEX API in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.