Giter Site home page Giter Site logo

mpi-node's Introduction

MPI implementation in node.js.
MPI is a message passing protocol implemented as a library in most of modern programming languages.
MPI allows programmer to create a cluster of nodes - locally or distributed. Each node is assigned an individual tid - own id from 0 to n, where n is a size of the cluster. Every node executes the same code, but behaves differently depending on its id. The API in this MPI implementation is basic and the methods that can be used are:

MPI.init(callback);
//executes callback after the nodes connect with each other
MPI.rank();
// returns an unique id of the node which executes the code
MPI.size()
//returns the size of the cluster (the number of nodes)
MPI.recv(msgType, callback)
function callback(data){
}
//starts to listen on all messages with msgType type and invokes callback on each message with the content as first parameter
MPI.broadcast({type: msgType, content: data});
//sends the data object to all the nodes that are listeing to msgType event
MPI.send(nodeId,{type: msgType, content: data});
//sends the data object only to a node with tid equal to nodeId. The type of this message is msgType.

You need a hosts.json file in a root directory (with at least the localhost entry - local machine must be named localhost, not via IP address). It has to be a valid JSON file - with the keys in " ".

    "hosts" :
    [
        {
            "hostname": "localhost",
            "processes": 2
        },
        {
            "hostname": "192.168.1.10",
            "processes": 3
        }
    ],
    "mainFile" : "app.js"

Check a client for mpi-node - mpi-node-cli - for using mpi-node as a cluster of distributed nodes. To use remote machines in the cluster, the mpi-node-cli has to be run there. npi-node-cli doesnt need to be run on localhost and in fact shouldn't), only on remote machines. usage:

$ npm install -g mpi-node-cli
$ mpi-node-cli

Package requires port 5666 open. It is only available on LINUX systems.

Available for node.js version 4.x or upper.

Basic example of 3 phase commit algorithm using mpi-node library:

const MPI = require('mpi-node');
MPI.init(main);
function main () {
    const tid = MPI.rank();
    const timeoutTime = 5000;
    let state = 'Begin';
    let numOfAnswers = 0;
    let timeout;
    if (tid === 0) {
        sendCommitRequest();
        setInterval(sendCommitRequest, 1000);
        function sendCommitRequest () {
            MPI.broadcast({type: 'CanCommit', content: 'transaction'});
            state = 'Waiting';
            timeout = setTimeout(() => {
                MPI.broadcast({type: 'Abort', content: 'transaction'});
                state = 'Aborted';
            }, timeoutTime);
        }
        MPI.recv('Yes', (message) => {
            numOfAnswers++;
            if(numOfAnswers === MPI.size() - 1){
                numOfAnswers = 0;
                clearTimeout(timeout);
                MPI.broadcast({type: 'preCommit', content: 'transaction'});
                state = 'Prepared';
                timeout = setTimeout(() => {
                    MPI.broadcast({type: 'Abort', content: 'transaction'});
                    state = 'Aborted';
                }, timeoutTime);
            }
        })
        MPI.recv('No', (message) => {
            MPI.broadcast({type: 'Abort', content: 'transaction'});
            state = 'Aborted';
        })
        MPI.recv('ACK', (message) => {
            numOfAnswers++;
            if(numOfAnswers === MPI.size() - 1){
                numOfAnswers = 0;
                clearTimeout(timeout);
                MPI.broadcast({type: 'doCommit', content: 'transaction'});
                state = 'Commited';
            }
        });
    }
    else {
        MPI.recv('CanCommit', (message) => {
            timeout = setTimeout(abortHandler, timeoutTime);
            MPI.send(0, {type: 'Yes', content: 'transaction'});
            state = 'Waiting';
        })
        MPI.recv('abort', abortHandler);
        MPI.recv('preCommit', (message) => {
            clearTimeout(timeout);
            timeout = setTimeout(commitHandler, timeoutTime);
            MPI.send(0, {type: 'ACK', content: 'transaction'});
            state = 'Prepared';
        })
        MPI.recv('doCommit', commitHandler);
    }
    function commitHandler () {
        if (timeout) {
            clearTimeout(timeout);
        }
        state = 'Commited';
    }
    function abortHandler(message){
        if (timeout) {
            clearTimeout(timeout);
            timeout = null;
        }
        state = 'Aborted';
    }

mpi-node's People

Contributors

rturaj avatar tomek-cf avatar vatras avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mpi-node's Issues

Example doesn't work

Example doesn't work, missing closing }. And also the class structure is broken.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.