2012-11-09 17:31:33 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2019-06-06 05:28:23 +00:00
|
|
|
Copyright (c) 2006-2019 Chung, Hyung-Hwan. All rights reserved.
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
2012-11-09 17:31:33 +00:00
|
|
|
|
2014-11-19 14:42:24 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2012-11-09 17:31:33 +00:00
|
|
|
*/
|
|
|
|
|
2013-02-01 14:30:25 +00:00
|
|
|
#include "mod-mpi.h"
|
2012-10-31 13:51:32 +00:00
|
|
|
#include <qse/cmn/str.h>
|
2012-11-01 06:42:38 +00:00
|
|
|
#include <qse/cmn/main.h>
|
2012-10-31 13:51:32 +00:00
|
|
|
|
2012-11-01 06:42:38 +00:00
|
|
|
#if defined(HAVE_MPI)
|
|
|
|
# include <mpi.h>
|
|
|
|
#else
|
|
|
|
# error this module needs mpi
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int fnc_size (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
qse_awk_val_t* retv;
|
|
|
|
int rank;
|
|
|
|
|
|
|
|
MPI_Comm_size (MPI_COMM_WORLD, &rank);
|
|
|
|
retv = qse_awk_rtx_makeintval (rtx, rank);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fnc_rank (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
qse_awk_val_t* retv;
|
|
|
|
int rank;
|
|
|
|
|
|
|
|
MPI_Comm_rank (MPI_COMM_WORLD, &rank);
|
|
|
|
retv = qse_awk_rtx_makeintval (rtx, rank);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-31 13:51:32 +00:00
|
|
|
|
|
|
|
static int fnc_hash (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
qse_awk_val_t* retv;
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_int_t hv;
|
2012-10-31 13:51:32 +00:00
|
|
|
|
|
|
|
hv = qse_awk_rtx_hashval (rtx, qse_awk_rtx_getarg (rtx, 0));
|
|
|
|
|
|
|
|
retv = qse_awk_rtx_makeintval (rtx, hv);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fnc_assign (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
qse_awk_val_t* retv;
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_int_t limit;
|
2012-10-31 13:51:32 +00:00
|
|
|
int rx;
|
|
|
|
|
2013-11-05 14:16:45 +00:00
|
|
|
rx = qse_awk_rtx_valtoint (rtx, qse_awk_rtx_getarg (rtx, 0), &limit);
|
2012-10-31 13:51:32 +00:00
|
|
|
if (rx >= 0)
|
|
|
|
{
|
|
|
|
qse_awk_nrflt_t nrflt;
|
2012-11-01 06:42:38 +00:00
|
|
|
int size, rank;
|
2012-10-31 13:51:32 +00:00
|
|
|
|
2012-11-01 06:42:38 +00:00
|
|
|
MPI_Comm_size (MPI_COMM_WORLD, &size);
|
|
|
|
MPI_Comm_rank (MPI_COMM_WORLD, &rank);
|
2012-10-31 13:51:32 +00:00
|
|
|
nrflt.limit = limit;
|
2012-11-01 06:42:38 +00:00
|
|
|
nrflt.size = size;
|
|
|
|
nrflt.rank = rank;
|
2012-10-31 13:51:32 +00:00
|
|
|
qse_awk_rtx_setnrflt (rtx, &nrflt);
|
|
|
|
}
|
|
|
|
|
|
|
|
retv = qse_awk_rtx_makeintval (rtx, rx);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fnc_reduce (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
2012-11-01 06:42:38 +00:00
|
|
|
qse_awk_val_t* retv;
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_int_t opidx, lv;
|
2013-11-04 02:33:52 +00:00
|
|
|
qse_awk_flt_t rv;
|
2012-10-31 13:51:32 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
static MPI_Op optab[] =
|
|
|
|
{
|
|
|
|
MPI_MIN,
|
|
|
|
MPI_MAX,
|
|
|
|
MPI_SUM,
|
|
|
|
MPI_PROD,
|
|
|
|
MPI_LAND,
|
|
|
|
MPI_LOR
|
|
|
|
};
|
|
|
|
|
2013-11-05 14:16:45 +00:00
|
|
|
if (qse_awk_rtx_valtoint (rtx, qse_awk_rtx_getarg (rtx, 1), &opidx) <= -1 ||
|
2012-11-01 06:42:38 +00:00
|
|
|
(opidx < 0 || opidx >= QSE_COUNTOF(optab)) ||
|
|
|
|
(n = qse_awk_rtx_valtonum (rtx, qse_awk_rtx_getarg (rtx, 0), &lv, &rv)) <= -1) goto softfail;
|
2012-10-31 13:51:32 +00:00
|
|
|
|
2013-11-05 14:16:45 +00:00
|
|
|
/* TODO: determine it to be MPI_LONG or MPI_INT, OR MPI_LONG_LONG_INT depending on the size of qse_awk_int_t */
|
2012-10-31 13:51:32 +00:00
|
|
|
if (n == 0)
|
|
|
|
{
|
2013-11-05 14:16:45 +00:00
|
|
|
qse_awk_int_t lout;
|
2012-11-01 06:42:38 +00:00
|
|
|
if (MPI_Allreduce (&lv, &lout, 1, MPI_LONG_LONG_INT, optab[opidx], MPI_COMM_WORLD) != MPI_SUCCESS) goto softfail;
|
|
|
|
retv = qse_awk_rtx_makeintval (rtx, lout);
|
2012-10-31 13:51:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-04 02:33:52 +00:00
|
|
|
qse_awk_flt_t fout;
|
2012-11-01 06:42:38 +00:00
|
|
|
if (MPI_Allreduce (&rv, &fout, 1, MPI_LONG_DOUBLE, optab[opidx], MPI_COMM_WORLD) != MPI_SUCCESS) goto softfail;
|
|
|
|
retv = qse_awk_rtx_makefltval (rtx, fout);
|
2012-10-31 13:51:32 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 06:42:38 +00:00
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
2012-10-31 13:51:32 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
softfail:
|
2012-11-01 06:42:38 +00:00
|
|
|
/* return without setting the return value.
|
|
|
|
* this intrinsic function will return a nil value when it fails */
|
2012-10-31 13:51:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fnc_barrier (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
int rx;
|
|
|
|
qse_awk_val_t* retv;
|
|
|
|
|
|
|
|
rx = (MPI_Barrier (MPI_COMM_WORLD) == MPI_SUCCESS)? 0: -1;
|
|
|
|
|
|
|
|
retv = qse_awk_rtx_makeintval (rtx, rx);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
typedef struct fnctab_t fnctab_t;
|
|
|
|
struct fnctab_t
|
|
|
|
{
|
|
|
|
const qse_char_t* name;
|
|
|
|
qse_awk_mod_sym_fnc_t info;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct inttab_t inttab_t;
|
|
|
|
struct inttab_t
|
|
|
|
{
|
|
|
|
const qse_char_t* name;
|
|
|
|
qse_awk_mod_sym_int_t info;
|
|
|
|
};
|
|
|
|
|
|
|
|
static fnctab_t fnctab[] =
|
|
|
|
{
|
2012-11-05 05:20:12 +00:00
|
|
|
{ QSE_T("assign"), { { 1, 1, QSE_NULL }, fnc_assign, 0 } },
|
|
|
|
{ QSE_T("barrier"), { { 0, 0, QSE_NULL }, fnc_barrier, 0 } },
|
|
|
|
{ QSE_T("hash"), { { 1, 1, QSE_NULL }, fnc_hash, 0 } },
|
|
|
|
{ QSE_T("rank"), { { 0, 0, QSE_NULL }, fnc_rank, 0 } },
|
|
|
|
{ QSE_T("reduce"), { { 2, 2, QSE_NULL }, fnc_reduce, 0 } },
|
|
|
|
{ QSE_T("size"), { { 0, 0, QSE_NULL }, fnc_size, 0 } }
|
2012-11-01 06:42:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inttab_t inttab[] =
|
|
|
|
{
|
|
|
|
{ QSE_T("REDUCE_LAND"), { 4 } },
|
|
|
|
{ QSE_T("REDUCE_LOR"), { 5 } },
|
|
|
|
{ QSE_T("REDUCE_MAX"), { 1 } },
|
|
|
|
{ QSE_T("REDUCE_MIN"), { 0 } },
|
|
|
|
{ QSE_T("REDUCE_PROD"), { 3 } },
|
|
|
|
{ QSE_T("REDUCE_SUM"), { 2 } }
|
2012-10-31 13:51:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qse_awk_mod_sym_t* sym)
|
|
|
|
{
|
2014-07-08 14:30:42 +00:00
|
|
|
qse_cstr_t ea;
|
2019-05-09 08:29:00 +00:00
|
|
|
int left, right, mid, n;
|
2012-10-31 13:51:32 +00:00
|
|
|
|
2012-11-05 09:49:54 +00:00
|
|
|
left = 0; right = QSE_COUNTOF(fnctab) - 1;
|
|
|
|
|
|
|
|
while (left <= right)
|
2012-10-31 13:51:32 +00:00
|
|
|
{
|
2017-05-02 01:14:32 +00:00
|
|
|
mid = left + (right - left) / 2;
|
2012-11-05 09:49:54 +00:00
|
|
|
|
|
|
|
n = qse_strcmp (fnctab[mid].name, name);
|
2012-11-28 14:12:26 +00:00
|
|
|
if (n > 0) right = mid - 1;
|
2012-11-05 09:49:54 +00:00
|
|
|
else if (n < 0) left = mid + 1;
|
|
|
|
else
|
2012-10-31 13:51:32 +00:00
|
|
|
{
|
|
|
|
sym->type = QSE_AWK_MOD_FNC;
|
2012-11-05 09:49:54 +00:00
|
|
|
sym->u.fnc = fnctab[mid].info;
|
2012-10-31 13:51:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 14:12:26 +00:00
|
|
|
}
|
2012-10-31 13:51:32 +00:00
|
|
|
|
2012-11-05 09:49:54 +00:00
|
|
|
left = 0; right = QSE_COUNTOF(inttab) - 1;
|
|
|
|
while (left <= right)
|
2012-10-31 13:51:32 +00:00
|
|
|
{
|
2017-05-02 01:14:32 +00:00
|
|
|
mid = left + (right - left) / 2;
|
2012-11-05 09:49:54 +00:00
|
|
|
|
|
|
|
n = qse_strcmp (inttab[mid].name, name);
|
2012-11-28 14:12:26 +00:00
|
|
|
if (n > 0) right = mid - 1;
|
2012-11-05 09:49:54 +00:00
|
|
|
else if (n < 0) left = mid + 1;
|
|
|
|
else
|
2012-10-31 13:51:32 +00:00
|
|
|
{
|
|
|
|
sym->type = QSE_AWK_MOD_INT;
|
2012-11-05 09:49:54 +00:00
|
|
|
sym->u.in = inttab[mid].info;
|
2012-10-31 13:51:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 14:12:26 +00:00
|
|
|
}
|
2012-10-31 13:51:32 +00:00
|
|
|
|
|
|
|
ea.ptr = name;
|
|
|
|
ea.len = qse_strlen(name);
|
|
|
|
qse_awk_seterror (awk, QSE_AWK_ENOENT, &ea, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: proper resource management */
|
|
|
|
|
2012-11-01 06:42:38 +00:00
|
|
|
static int init (qse_awk_mod_t* mod, qse_awk_rtx_t* rtx)
|
2012-10-31 13:51:32 +00:00
|
|
|
{
|
2012-11-01 06:42:38 +00:00
|
|
|
/* TODO: anything */
|
2012-10-31 13:51:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-01 06:42:38 +00:00
|
|
|
static void fini (qse_awk_mod_t* mod, qse_awk_rtx_t* rtx)
|
2012-10-31 13:51:32 +00:00
|
|
|
{
|
|
|
|
/* TODO: anything */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unload (qse_awk_mod_t* mod, qse_awk_t* awk)
|
|
|
|
{
|
|
|
|
/* TODO: anything */
|
|
|
|
}
|
|
|
|
|
2013-02-01 14:30:25 +00:00
|
|
|
int qse_awk_mod_mpi (qse_awk_mod_t* mod, qse_awk_t* awk)
|
2012-10-31 13:51:32 +00:00
|
|
|
{
|
|
|
|
mod->query = query;
|
|
|
|
mod->unload = unload;
|
|
|
|
|
|
|
|
mod->init = init;
|
|
|
|
mod->fini = fini;
|
|
|
|
/*
|
|
|
|
mod->ctx...
|
|
|
|
*/
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-01 06:42:38 +00:00
|
|
|
/* The MPI module is special in that it exports 2 extra symbols -
|
|
|
|
* mpi_init and mpi_fini. These two symbols are intended to be called
|
|
|
|
* dynamically using dlopen() or something similar when an application
|
|
|
|
* intending to use mpi::xxx starts up. This way, the application doesn't
|
|
|
|
* have to be linked to any MPI libraries while this module is linked to
|
|
|
|
* an MPI library. If this module doesn't exist, it means MPI is not availble
|
|
|
|
* and the module wasn't built. So you can't access mpi::xxx symbols either
|
|
|
|
*/
|
|
|
|
|
2019-05-14 03:16:33 +00:00
|
|
|
QSE_EXPORT int qse_awk_mod_mpi_init (int argc, qse_achar_t* argv[])
|
2012-11-01 06:42:38 +00:00
|
|
|
{
|
|
|
|
int rx;
|
|
|
|
|
2019-05-13 16:35:33 +00:00
|
|
|
if (MPI_Init(&argc, &argv) != MPI_SUCCESS) rx = -1;
|
2012-11-01 06:42:38 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
MPI_Comm_set_errhandler (MPI_COMM_WORLD, MPI_ERRORS_RETURN);
|
|
|
|
rx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rx;
|
|
|
|
}
|
|
|
|
|
2019-05-14 03:16:33 +00:00
|
|
|
QSE_EXPORT void qse_awk_mod_mpi_fini (void)
|
2012-11-01 06:42:38 +00:00
|
|
|
{
|
|
|
|
MPI_Finalize ();
|
|
|
|
}
|