Intel® C++ Compiler 16.0 User and Reference Guide

Setting Stack Size on Coprocessors

This topic only applies to Intel® Many Integrated Core Architecture (Intel® MIC Architecture).

Each CPU thread that offloads code executes the offloaded code in a coprocessor thread dedicated to that CPU thread. The default stack size for that coprocessor thread is fairly small, 2 MB.

To increase the stack size allocated to offloaded code, set the environment variable MIC_STACKSIZE to a size sufficient for the offloaded code. MIC_STACKSIZE is always honored. It does not require the variable MIC_ENV_PREFIX to be set.

Specify the value for MIC_STACKSIZE as follows:

integerB

A value, in bytes.

integerK

A value, in kilobytes.

integerM

A value, in megabytes.

integerG

A value, in gigabytes.

integerT

A value, in terabytes.

For example, the following shell BASH and C shell commands set the maximum stacksize for the main coprocessor thread to 50MB:

export MIC_STACKSIZE=50M    #(BASH shell)
setenv MIC_STACKSIZE  50M    #(C shell)

Note

OpenMP* and Intel® Cilk™ Plus runtime libraries have their own environment variables for adjusting the sizes of the stacks used by additional OpenMP or Intel® Cilk™ Plus threads. MIC_STACKSIZE does not apply to such threads. For example, to increase the available stack size for every OpenMP thread except the main thread to 3 MB using the BASH shell, enter: export OMP_STACKSIZE=3M (affects OpenMP threads on both host and coprocessor) or export MIC_ENV_PREFIX=ABC export ABC_OMP_STACKSIZE=3M (affects coprocessor OpenMP threads only).

If you use pthreads, you can set the pthreads stack size by using posix_memalign() to allocate a page-aligned stack (4096 byte page) with one extra guard page at the bottom, and then passing that stack to pthread_attr_setstack(). Then, use pthread_attr_setstack() for pthread_create(). The following code demonstrates this method.

#define PAGESIZE 4096

void *makestack(int size) {
    int ret;
    void *space;
    char *stack;

    /* size in pages */
    ret = posix_memalign(&space, PAGESIZE, (size+1) * PAGESIZE);
    if (ret != 0) {
        fprintf(stderr, "posix_memalign returns %d\n", ret);
        return 0;
    }

    mprotect(space, PAGESIZE, PROT_NONE);
    stack = (char *)space + PAGESIZE;
    printf("makestack: size %d stack %p space %p\n",
        size, stack, space);
    return stack;
}

void setstack(int argc, char **argv) {
    void *i;
    pthread_t t;
    int ret;
    const int stacksize = (12 * 1024 * 1024) / PAGESIZE;

    pthread_attr_init(&thread_attrs);
    pthread_attr_setstack(&thread_attrs,
        makestack(stacksize), stacksize * PAGESIZE);
    pthread_create(&t, &thread_attrs, chkstack, (void *)stacksize);
}

See Also