## shmemalign

#### Purpose

Allocates a symmetric, remotely accessible object from the Symmetric heap with specified byte alignment.

#### C syntax

    #include <shmem.h>

    void *shmemalign(size_t alignment, size_t size);

#### Parameters

INPUT
alignment
  The desired byte alignment of the symmetric object.
size
  The minimum desired size of the symmetric object.

#### Description

The shmemalign function returns a pointer to a remotely accessible memory block of minimum size bytes with specified byte alignment from the symmetric heap. The allocated object can be used by IBM openshmem communication functions. It returns NULL if the symmetric heap has run out of space.

It is required that all PEs must participate in the memory allocation and with the identical parameter. Internally, shmem_barrier_all is called before returning from the function, thus the allocated object can be used right away on all PEs. User has to ensure the same size parameter is used on all PEs when calling the function; otherwise, subsequent calls to the symmetric object allocation functions may not return the same address.

#### C examples

    #include <stdio.h>
    #include <string.h>
    #include <strings.h>

    #include <shmem.h>

    int main (int argc, char* argv[])
    {
        int total_tasks = -1;
        int my_task = -1;

        start_pes(0);

        total_tasks = _num_pes();

        if (total_tasks <= 0) {
            printf("FAILED\n");
            return 0;
        } else {
            printf("number of pes is %d\n", total_tasks);
        }

        my_task = _my_pe();

        if (my_task < 0){
            printf("FAILED\n");
            return 0;
        } else {
            printf("my pe id is %d\n", my_task);
        }

        int *addrs[10];
        for (int i=0; i<10; i++) {
            addrs[i] = (int *) shmalloc(sizeof(int)*1024);
            printf("addrs[%d] is %p after malloc\n", i, addrs[i]);
            bzero(addrs[i], sizeof(int)*1024);
            for (int j=0; j<1024; j++) {
                addrs[i][j] = j;
            }
        }

        for (int i=0; i<10; i++) {
            shfree(addrs[i]);
        }

        int *addr = NULL;
        addr = (int *) shmalloc(sizeof(int)*1024);
        printf("addr is %p after malloc\n", addr);

        addr = (int *) shrealloc(addr, sizeof(int)*1024);
        printf("addr is %p after realloc\n", addr);

        shfree(addr);

        for (int i=0; i<10; i++) {
            addrs[i] = (int *) shmemalign(0x2000, sizeof(int)*1024);
            printf("addrs[%d] is %p after memalign\n", i, addrs[i]);
        }

        for (int i=0; i<10; i++) {
            shfree(addrs[i]);
        }

        printf("PASSED\n");
        return 0;
    }

#### Related information

Subroutines: shfree, shmalloc, shrealloc

OpenSHMEM API Index