Debug for ArrayFire

[Old posts from the commercial version of ArrayFire] Discussion of ArrayFire using CUDA or OpenCL.

Moderator: pavanky

Debug for ArrayFire

Postby chfeilong » Tue Nov 12, 2013 11:29 pm

it' s diffcult to debug in arrayfire,when my code is over 100lines,set breakpoint doesn't work ,nsight seems not work on arrayfire,Does there good debug tool for arrayfire ?
chfeilong
 
Posts: 104
Joined: Sun Aug 15, 2010 11:28 pm

Re: Debug for ArrayFire

Postby umar456 » Sat Nov 16, 2013 8:40 pm

Hi chfeilong,

ArrayFire has a couple of features which sacrifice debugging in favor of speed. On of these features is the Just-in Time(JIT) compilation. The JIT allows us to create custom kernels at run-time and defer execution of those kernels until the data is necessary. This makes things difficult to debug because not all variables are updated when you want to look at the values in the debugger. This feature is a critical performance feature which would drastically hurt performance if it was turned off.

It is therefore necessary to debug at run-time. There are techniques you can use to debug an ArrayFire program. ArrayFire comes with an excellent graphics library which will allow you to visualize the data you are working on. For example you can view an image in your program by calling the image function.

Code: Select all
array img = loadimage("my_image.png");
image(img);
getchar();


These lines will open a window with your image that you loaded.

If you need to get specific values of your data, you can use the print macro. This will print all the values on the console. Printing all the values might not be convenient if you are working on a large data-set. For this reason we have created a macro/function which will print the first eight(8) elements in an array along with its dimensions:

Code: Select all
// print the top N values of matrices/vectors
#define top(exp)  do { _top(#exp, exp); } while (0)
static void _top(const char* exp, array X) {
    int elements = 8;
    if (X.isvector()) {
        printf("%s(0:%d) =    dims=[%d %d", exp, elements - 1, X.dims(0), X.dims(1));
        for (int i = 2; i < X.numdims(); ++i) { printf(" %d", X.dims(i)); }
        printf("]\n");
        X = X(seq(std::min(elements, X.elements())));
    } else {
        unsigned rows = std::min(elements, X.dims(0));
        unsigned cols = std::min(elements, X.dims(1));
        printf("%s(0:%d,0:%d) =    dims=[%d %d", exp, rows - 1, cols - 1, X.dims(0), X.dims(1));
        for (int i = 2; i < X.numdims(); ++i) { printf(" %d", X.dims(i)); }
        printf("]\n");
        X = X(seq(rows), seq(cols));
    }
    print_(NULL, X);
}


We use this function extensively internally when we are working on a project for customers.

There are often times when you need to get basic statistics for an array. The stats function here will display basic statistics for your data set.

Code: Select all
// print min and max of array
#define stats(exp)      _stats(#exp, exp)
#define stats_idx(exp)  _stats_idx(#exp, exp)
static void _stats(const char* exp_str, const array& exp) {
    printf("%-20s   [%-12g  %-12g  %-12g]\n", exp_str,
           min<float>(exp), mean<float>(exp), max<float>(exp));
}


These functions allow us to handle most of the debugging issues we run into.

Can you elaborate how you are using Nsight. Is it to profile your application? We use Nsight and NVIDIA Visual Profile daily to profile ArrayFire programs.

Umar
User avatar
umar456
 
Posts: 7
Joined: Fri Nov 16, 2012 12:24 pm

Re: Debug for ArrayFire

Postby chfeilong » Sun Nov 17, 2013 8:50 am

I want to fix the debug in my code :
viewtopic.php?f=17&t=37307
chfeilong
 
Posts: 104
Joined: Sun Aug 15, 2010 11:28 pm


Return to [archive-commercial] Programming & Development with ArrayFire