Skip to content

Examples

Keith-Bateman edited this page Jul 29, 2019 · 2 revisions

This page contains some basic example code demonstrating how to use Basket.

Queue

This is the simplest possible example, taken straight from test/simple_test.cpp, but with some added comments. We push one element to a queue and then pop that element.

#include <basket.h>
#include <iostream>
#include <mpi.h>

int main(int argc, char **argv) {
    MPI_Init(&argc, &argv);
    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    int my_server = 0;
    /**
     * basket::queue<MappedType>(name, is_server,
     * MappedType: The type of values of this Basket
     * my_server, num_servers, server_on_node)
     * name: should be unique to this structure
     * is_server: indicates whether we're initializing
     * a server or client to this Basket.
     * my_server: Which server we access; note that
     * servers are indexed separately from clients,
     * so if we had servers on processes 1, 3, and 6,
     * then the indices of those servers would be 0, 1,
     * and 2 respectively.
     * num_servers: How many servers does this Basket
     * consist of. In this example we only have one.
     * server_on_node: Is the server for this Basket
     * on the current node? If we're initializing a
     * server then this is trivially true (as well as
     * my_server being the rank of the server as it
     * relates to the other servers). If we initialize
     * a client then this should be true if my_server
     * is on the same node as the client. If it's
     * false for a client, then it's ideal for the
     * server to be closer to the client, since servers
     * are the only parts of the system that store
     * data.
     */
    basket::queue<int> int_queue("QUEUE", rank == my_server, my_server, 1, true);
    if (rank == my_server) {
        /**
         * WaitForElement waits for an element
         * to be pushed to my_server
         */
        int_queue.WaitForElement(my_server);
        /**
         * Pop an element from my_server
         */
        auto result = int_queue.Pop(my_server);
        if (result.first) {
           std::cout << result.second << std::endl;
        }
    } else {
        /**
         * Push an element to my_server
         */
        int_queue.Push(42, my_server);
    }
    MPI_Finalize();
}
Clone this wiki locally