Step by Step Examples

Transform easily your code to use SwLoc !

The beginning

When you want to use SwLoc for the first time, you have 2 or more codes and each has its own main.

Each has also its own topology detection based on HwLoc and basically use the entire set of resources of the machine by creating threads with pthread.

File one.c
                                        
int main(int argc, char ** argv)
{
        //Do some interesting things

        return EXIT_SUCCESS;
}
                                        
                                
File two.c
                                        
int main(int argc, char ** argv)
{
        //Do also some interesting things

        return EXIT_SUCCESS;
}
                                        
                                

The first thing you need to do is to rename each main function.

For this example, we add the filename at the end of the main.

File one.c
                                        
int main_one(int argc, char ** argv)
{
        //Do some interesting things

        return EXIT_SUCCESS;
}
                                        
                                
File two.c
                                        
int main_two(int argc, char ** argv)
{
        //Do also some interesting things

        return EXIT_SUCCESS;
}
                                        
                                

Then, it's time to firstly use SwLoc ! We will do it together step by step !

Let's begin by creating a main function and initialize (and also finalize) SwLoc.

File swloc.c
                                        
int main(int argc, char ** argv)
{
        swloc_init();

        swloc_finalize();

        return EXIT_SUCCESS;
}
                                        
                                

Now, it's time to think about how many parts do you want to divide your machine

Here, we want to run two codes, with the same resources on the machine. Therefore, we create two contexts with the same number of cores. The function to create the context is swloc_context_create(). It can take a list of parameters ended by SWLOC_CONTEXT_END. We require the half of the computer with SWLOC_CONTEXT_NB_CPUS_HALF.

File swloc.c
                                        
int main(int argc, char ** argv)
{
        swloc_init();

        /* Divide the machine equally in two */
        swloc_context_t ctx1 = swloc_context_create(SWLOC_CONTEXT_NB_CPUS_HALF, SWLOC_CONTEXT_END);
        swloc_context_t ctx2 = swloc_context_create(SWLOC_CONTEXT_NB_CPUS_HALF, SWLOC_CONTEXT_END);

        /* Free everything */
        swloc_context_destroy(ctx1);
        swloc_context_destroy(ctx2);

        swloc_finalize();

        return EXIT_SUCCESS;
}
                                        
                                

After creating the contexts, we have to specify which code we want to execute and on which context.

We use for that purpose, the structure swloc_kernel_options. In each structure, you can describe the function to execute, the number of parameters, and the parameters themselves like a main function.

Then, give this structure and the context to the function swloc_kernel_start() to start the computation.

Don't forget to wait until it will be finished with the function swloc_kernel_wait() !

File swloc.c
                                        
int main(int argc, char ** argv)
{
        swloc_init();

        /* Divide the machine equally in two */
        swloc_context_t ctx1 = swloc_context_create(SWLOC_CONTEXT_NB_CPUS_HALF, SWLOC_CONTEXT_END);
        swloc_context_t ctx2 = swloc_context_create(SWLOC_CONTEXT_NB_CPUS_HALF, SWLOC_CONTEXT_END);

        struct swloc_kernel_options opt1, opt2;

        /* Select options to launch func1 with parameters argc and argv */
        swloc_kernel_options_init(&opt1);
        opt1.func_main = func1;
        opt1.argc = argc;
        opt1.argv = argv;

        /* Select options to launch func2 with parameters argc and argv */
        swloc_kernel_options_init(&opt2);
        opt2.func_main = func2;
        opt2.argc = argc;
        opt2.argv = argv;

        /* Launch the computation on each context */
        swloc_kernel_t knl1 = swloc_kernel_start(ctx1, &opt1);
        swloc_kernel_t knl2 = swloc_kernel_start(ctx2, &opt2);

        /* Wait them */
        swloc_kernel_wait(knl1);
        swloc_kernel_wait(knl2);

        /* Free everything */
        swloc_context_destroy(ctx1);
        swloc_context_destroy(ctx2);

        swloc_finalize();

        return EXIT_SUCCESS;
}
                                        
                                

So, now you are an expert of the basics of SwLoc, let's go to the next step : SwLoc with runtimes !

Download

Do you want to use or just to try SwLoc ?
Click here to access to the Git repository !