From c00c46f97638869fa08ea3a2a294e8f0d4c5605d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Donval?= Date: Tue, 3 Oct 2023 13:40:13 +0100 Subject: [PATCH 1/4] Use size_t in mr2d_malloc In C, the type of the number of bytes passed to `malloc` *is* of type `size_t`. The current code ties that number of bytes to the general "Index type" (`Int`) which is defined at compile time and does fail on 32-bits systems setting `Int` as 64-bits. Additionally, this artificially limits 64-bit-element aux matrices to 2GB instead of 16GB with 32-bits signed indices. --- REDIST/SRC/pgemraux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/REDIST/SRC/pgemraux.c b/REDIST/SRC/pgemraux.c index 17dc301d..389bc497 100644 --- a/REDIST/SRC/pgemraux.c +++ b/REDIST/SRC/pgemraux.c @@ -105,7 +105,7 @@ extern void Cpigemr2d(); #include #include void * -mr2d_malloc(Int n) +mr2d_malloc(size_t n) { void *ptr; assert(n > 0); From 16b5f875fa0993bcf05b40fec96820c37bbea9d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Donval?= Date: Tue, 3 Oct 2023 14:24:19 +0100 Subject: [PATCH 2/4] Adds negative number detection in mr2d_malloc --- REDIST/SRC/pgemraux.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/REDIST/SRC/pgemraux.c b/REDIST/SRC/pgemraux.c index 389bc497..e078e07e 100644 --- a/REDIST/SRC/pgemraux.c +++ b/REDIST/SRC/pgemraux.c @@ -104,11 +104,12 @@ extern void Cpigemr2d(); #include #include #include +const size_t NEGFLAG = -1; void * mr2d_malloc(size_t n) { void *ptr; - assert(n > 0); + assert((n & NEGFLAG) == 0); ptr = (void *) malloc(n); if (ptr == NULL) { fprintf(stderr, "xxmr2d:out of memory\n"); From e2b4173637acc6ab2e8a6eb535ee26335f762ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Donval?= Date: Tue, 3 Oct 2023 16:40:35 +0100 Subject: [PATCH 3/4] Adds explicit size_t casts --- REDIST/SRC/pcgemr.c | 14 +++++++------- REDIST/SRC/pcgemr2.c | 2 +- REDIST/SRC/pctrmr.c | 14 +++++++------- REDIST/SRC/pctrmr2.c | 2 +- REDIST/SRC/pdgemr.c | 14 +++++++------- REDIST/SRC/pdgemr2.c | 2 +- REDIST/SRC/pdtrmr.c | 14 +++++++------- REDIST/SRC/pdtrmr2.c | 2 +- REDIST/SRC/pigemr.c | 14 +++++++------- REDIST/SRC/pigemr2.c | 2 +- REDIST/SRC/pitrmr.c | 14 +++++++------- REDIST/SRC/pitrmr2.c | 2 +- REDIST/SRC/psgemr.c | 14 +++++++------- REDIST/SRC/psgemr2.c | 2 +- REDIST/SRC/pstrmr.c | 14 +++++++------- REDIST/SRC/pstrmr2.c | 2 +- REDIST/SRC/pzgemr.c | 14 +++++++------- REDIST/SRC/pzgemr2.c | 2 +- REDIST/SRC/pztrmr.c | 15 ++++++++------- REDIST/SRC/pztrmr2.c | 2 +- 20 files changed, 81 insertions(+), 80 deletions(-) diff --git a/REDIST/SRC/pcgemr.c b/REDIST/SRC/pcgemr.c index bd6de2f9..d3a14825 100644 --- a/REDIST/SRC/pcgemr.c +++ b/REDIST/SRC/pcgemr.c @@ -342,7 +342,7 @@ Cpcgemr2d(m, n, assert((myprow1 < p1 && mypcol1 < q1) || (myprow1 == -1 && mypcol1 == -1)); /* exchange the missing parameters among the processors: shape of grids and * location of the processors */ - param = (Int *) mr2d_malloc(3 * (nprocs * 2 + NBPARAM) * sizeof(Int)); + param = (Int *) mr2d_malloc(3 * ((size_t)nprocs * 2 + NBPARAM) * sizeof(Int)); ra = param + nprocs * 2 + NBPARAM; ca = param + (nprocs * 2 + NBPARAM) * 2; for (i = 0; i < nprocs * 2 + NBPARAM; i++) @@ -467,10 +467,10 @@ Cpcgemr2d(m, n, /* allocing room for the tabs, alloc for the worst case,local_n or local_m * intervals, in fact the worst case should be less, perhaps half that,I * should think of that one day. */ - h_inter = (IDESC *) mr2d_malloc(DIVUP(ma->n, q0 * ma->nbcol) * - ma->nbcol * sizeof(IDESC)); - v_inter = (IDESC *) mr2d_malloc(DIVUP(ma->m, p0 * ma->nbrow) - * ma->nbrow * sizeof(IDESC)); + h_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->n, q0 * ma->nbcol)) * + (size_t)ma->nbcol * sizeof(IDESC)); + v_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->m, p0 * ma->nbrow)) + * (size_t)ma->nbrow * sizeof(IDESC)); /* We go for the scanning of indices. For each processor including mypnum, * we fill the sendbuff buffer (scanD0(SENDBUFF)) and when it is done send * it. Then for each processor, we compute the size of message to be @@ -570,7 +570,7 @@ init_chenille(Int mypnum, Int nprocs, Int n0, Int *proc0, Int n1, Int *proc1, In Int ns, nr, i, tot; Int *sender, *recver, *g0, *g1; tot = max(n0, n1); - sender = (Int *) mr2d_malloc((nprocs + tot) * sizeof(Int) * 2); + sender = (Int *) mr2d_malloc((size_t)(nprocs + tot) * sizeof(Int) * 2); recver = sender + tot; *psend = sender; *precv = recver; @@ -706,7 +706,7 @@ gridreshape(Int *ctxtp) Int i, j; ori = *ctxtp; Cblacs_gridinfo(ori, &nprow, &npcol, &myrow, &mycol); - usermap = mr2d_malloc(sizeof(Int) * nprow * npcol); + usermap = mr2d_malloc(sizeof(Int) * (size_t)nprow * (size_t)npcol); for (i = 0; i < nprow; i++) for (j = 0; j < npcol; j++) { usermap[i + j * nprow] = Cblacs_pnum(ori, i, j); diff --git a/REDIST/SRC/pcgemr2.c b/REDIST/SRC/pcgemr2.c index ce5370db..c6b20c85 100644 --- a/REDIST/SRC/pcgemr2.c +++ b/REDIST/SRC/pcgemr2.c @@ -121,7 +121,7 @@ setmemory(complex **adpointer, Int blocksize) return; } *adpointer = (complex *) mr2d_malloc( - blocksize * sizeof(complex)); + (size_t)blocksize * sizeof(complex)); } /******************************************************************/ /* Free the memory space after the malloc */ diff --git a/REDIST/SRC/pctrmr.c b/REDIST/SRC/pctrmr.c index 01225285..1516690a 100644 --- a/REDIST/SRC/pctrmr.c +++ b/REDIST/SRC/pctrmr.c @@ -359,7 +359,7 @@ Cpctrmr2d(uplo, diag, m, n, assert((myprow1 < p1 && mypcol1 < q1) || (myprow1 == -1 && mypcol1 == -1)); /* exchange the missing parameters among the processors: shape of grids and * location of the processors */ - param = (Int *) mr2d_malloc(3 * (nprocs * 2 + NBPARAM) * sizeof(Int)); + param = (Int *) mr2d_malloc(3 * ((size_t)nprocs * 2 + NBPARAM) * sizeof(Int)); ra = param + nprocs * 2 + NBPARAM; ca = param + (nprocs * 2 + NBPARAM) * 2; for (i = 0; i < nprocs * 2 + NBPARAM; i++) @@ -484,10 +484,10 @@ Cpctrmr2d(uplo, diag, m, n, /* allocing room for the tabs, alloc for the worst case,local_n or local_m * intervals, in fact the worst case should be less, perhaps half that,I * should think of that one day. */ - h_inter = (IDESC *) mr2d_malloc(DIVUP(ma->n, q0 * ma->nbcol) * - ma->nbcol * sizeof(IDESC)); - v_inter = (IDESC *) mr2d_malloc(DIVUP(ma->m, p0 * ma->nbrow) - * ma->nbrow * sizeof(IDESC)); + h_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->n, q0 * ma->nbcol)) * + (size_t)ma->nbcol * sizeof(IDESC)); + v_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->m, p0 * ma->nbrow)) + * (size_t)ma->nbrow * sizeof(IDESC)); /* We go for the scanning of indices. For each processor including mypnum, * we fill the sendbuff buffer (scanD0(SENDBUFF)) and when it is done send * it. Then for each processor, we compute the size of message to be @@ -592,7 +592,7 @@ init_chenille(Int mypnum, Int nprocs, Int n0, Int *proc0, Int n1, Int *proc1, In Int ns, nr, i, tot; Int *sender, *recver, *g0, *g1; tot = max(n0, n1); - sender = (Int *) mr2d_malloc((nprocs + tot) * sizeof(Int) * 2); + sender = (Int *) mr2d_malloc((size_t)(nprocs + tot) * sizeof(Int) * 2); recver = sender + tot; *psend = sender; *precv = recver; @@ -664,7 +664,7 @@ gridreshape(Int *ctxtp) Int i, j; ori = *ctxtp; Cblacs_gridinfo(ori, &nprow, &npcol, &myrow, &mycol); - usermap = mr2d_malloc(sizeof(Int) * nprow * npcol); + usermap = mr2d_malloc(sizeof(Int) * (size_t)nprow * (size_t)npcol); for (i = 0; i < nprow; i++) for (j = 0; j < npcol; j++) { usermap[i + j * nprow] = Cblacs_pnum(ori, i, j); diff --git a/REDIST/SRC/pctrmr2.c b/REDIST/SRC/pctrmr2.c index 99e34f9f..0e6d04b2 100644 --- a/REDIST/SRC/pctrmr2.c +++ b/REDIST/SRC/pctrmr2.c @@ -121,7 +121,7 @@ setmemory(complex **adpointer, Int blocksize) return; } *adpointer = (complex *) mr2d_malloc( - blocksize * sizeof(complex)); + (size_t)blocksize * sizeof(complex)); } /******************************************************************/ /* Free the memory space after the malloc */ diff --git a/REDIST/SRC/pdgemr.c b/REDIST/SRC/pdgemr.c index 706d7b20..2cc2d013 100644 --- a/REDIST/SRC/pdgemr.c +++ b/REDIST/SRC/pdgemr.c @@ -339,7 +339,7 @@ Cpdgemr2d(m, n, assert((myprow1 < p1 && mypcol1 < q1) || (myprow1 == -1 && mypcol1 == -1)); /* exchange the missing parameters among the processors: shape of grids and * location of the processors */ - param = (Int *) mr2d_malloc(3 * (nprocs * 2 + NBPARAM) * sizeof(Int)); + param = (Int *) mr2d_malloc(3 * ((size_t)nprocs * 2 + NBPARAM) * sizeof(Int)); ra = param + nprocs * 2 + NBPARAM; ca = param + (nprocs * 2 + NBPARAM) * 2; for (i = 0; i < nprocs * 2 + NBPARAM; i++) @@ -464,10 +464,10 @@ Cpdgemr2d(m, n, /* allocing room for the tabs, alloc for the worst case,local_n or local_m * intervals, in fact the worst case should be less, perhaps half that,I * should think of that one day. */ - h_inter = (IDESC *) mr2d_malloc(DIVUP(ma->n, q0 * ma->nbcol) * - ma->nbcol * sizeof(IDESC)); - v_inter = (IDESC *) mr2d_malloc(DIVUP(ma->m, p0 * ma->nbrow) - * ma->nbrow * sizeof(IDESC)); + h_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->n, q0 * ma->nbcol)) * + (size_t)ma->nbcol * sizeof(IDESC)); + v_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->m, p0 * ma->nbrow)) + * (size_t)ma->nbrow * sizeof(IDESC)); /* We go for the scanning of indices. For each processor including mypnum, * we fill the sendbuff buffer (scanD0(SENDBUFF)) and when it is done send * it. Then for each processor, we compute the size of message to be @@ -569,7 +569,7 @@ init_chenille(mypnum, nprocs, n0, proc0, n1, proc1, psend, precv, myrang) Int ns, nr, i, tot; Int *sender, *recver, *g0, *g1; tot = max(n0, n1); - sender = (Int *) mr2d_malloc((nprocs + tot) * sizeof(Int) * 2); + sender = (Int *) mr2d_malloc((size_t)(nprocs + tot) * sizeof(Int) * 2); recver = sender + tot; *psend = sender; *precv = recver; @@ -713,7 +713,7 @@ gridreshape(Int *ctxtp) Int i, j; ori = *ctxtp; Cblacs_gridinfo(ori, &nprow, &npcol, &myrow, &mycol); - usermap = mr2d_malloc(sizeof(Int) * nprow * npcol); + usermap = mr2d_malloc(sizeof(Int) * (size_t)nprow * (size_t)npcol); for (i = 0; i < nprow; i++) for (j = 0; j < npcol; j++) { usermap[i + j * nprow] = Cblacs_pnum(ori, i, j); diff --git a/REDIST/SRC/pdgemr2.c b/REDIST/SRC/pdgemr2.c index b6498cf2..a0305707 100644 --- a/REDIST/SRC/pdgemr2.c +++ b/REDIST/SRC/pdgemr2.c @@ -118,7 +118,7 @@ setmemory(double **adpointer, Int blocksize) return; } *adpointer = (double *) mr2d_malloc( - blocksize * sizeof(double)); + (size_t)blocksize * sizeof(double)); } /******************************************************************/ /* Free the memory space after the malloc */ diff --git a/REDIST/SRC/pdtrmr.c b/REDIST/SRC/pdtrmr.c index 7cf8d017..7381652d 100644 --- a/REDIST/SRC/pdtrmr.c +++ b/REDIST/SRC/pdtrmr.c @@ -356,7 +356,7 @@ Cpdtrmr2d(uplo, diag, m, n, assert((myprow1 < p1 && mypcol1 < q1) || (myprow1 == -1 && mypcol1 == -1)); /* exchange the missing parameters among the processors: shape of grids and * location of the processors */ - param = (Int *) mr2d_malloc(3 * (nprocs * 2 + NBPARAM) * sizeof(Int)); + param = (Int *) mr2d_malloc(3 * ((size_t)nprocs * 2 + NBPARAM) * sizeof(Int)); ra = param + nprocs * 2 + NBPARAM; ca = param + (nprocs * 2 + NBPARAM) * 2; for (i = 0; i < nprocs * 2 + NBPARAM; i++) @@ -481,10 +481,10 @@ Cpdtrmr2d(uplo, diag, m, n, /* allocing room for the tabs, alloc for the worst case,local_n or local_m * intervals, in fact the worst case should be less, perhaps half that,I * should think of that one day. */ - h_inter = (IDESC *) mr2d_malloc(DIVUP(ma->n, q0 * ma->nbcol) * - ma->nbcol * sizeof(IDESC)); - v_inter = (IDESC *) mr2d_malloc(DIVUP(ma->m, p0 * ma->nbrow) - * ma->nbrow * sizeof(IDESC)); + h_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->n, q0 * ma->nbcol)) * + (size_t)ma->nbcol * sizeof(IDESC)); + v_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->m, p0 * ma->nbrow)) + * (size_t)ma->nbrow * sizeof(IDESC)); /* We go for the scanning of indices. For each processor including mypnum, * we fill the sendbuff buffer (scanD0(SENDBUFF)) and when it is done send * it. Then for each processor, we compute the size of message to be @@ -589,7 +589,7 @@ init_chenille(Int mypnum, Int nprocs, Int n0, Int *proc0, Int n1, Int *proc1, In Int ns, nr, i, tot; Int *sender, *recver, *g0, *g1; tot = max(n0, n1); - sender = (Int *) mr2d_malloc((nprocs + tot) * sizeof(Int) * 2); + sender = (Int *) mr2d_malloc((size_t)(nprocs + tot) * sizeof(Int) * 2); recver = sender + tot; *psend = sender; *precv = recver; @@ -661,7 +661,7 @@ gridreshape(Int *ctxtp) Int i, j; ori = *ctxtp; Cblacs_gridinfo(ori, &nprow, &npcol, &myrow, &mycol); - usermap = mr2d_malloc(sizeof(Int) * nprow * npcol); + usermap = mr2d_malloc(sizeof(Int) * (size_t)nprow * (size_t)npcol); for (i = 0; i < nprow; i++) for (j = 0; j < npcol; j++) { usermap[i + j * nprow] = Cblacs_pnum(ori, i, j); diff --git a/REDIST/SRC/pdtrmr2.c b/REDIST/SRC/pdtrmr2.c index 65e970f9..c35085f1 100644 --- a/REDIST/SRC/pdtrmr2.c +++ b/REDIST/SRC/pdtrmr2.c @@ -118,7 +118,7 @@ setmemory(double **adpointer, Int blocksize) return; } *adpointer = (double *) mr2d_malloc( - blocksize * sizeof(double)); + (size_t)blocksize * sizeof(double)); } /******************************************************************/ /* Free the memory space after the malloc */ diff --git a/REDIST/SRC/pigemr.c b/REDIST/SRC/pigemr.c index e9e0f99e..ddcaf576 100644 --- a/REDIST/SRC/pigemr.c +++ b/REDIST/SRC/pigemr.c @@ -339,7 +339,7 @@ Cpigemr2d(m, n, assert((myprow1 < p1 && mypcol1 < q1) || (myprow1 == -1 && mypcol1 == -1)); /* exchange the missing parameters among the processors: shape of grids and * location of the processors */ - param = (Int *) mr2d_malloc(3 * (nprocs * 2 + NBPARAM) * sizeof(Int)); + param = (Int *) mr2d_malloc(3 * ((size_t)nprocs * 2 + NBPARAM) * sizeof(Int)); ra = param + nprocs * 2 + NBPARAM; ca = param + (nprocs * 2 + NBPARAM) * 2; for (i = 0; i < nprocs * 2 + NBPARAM; i++) @@ -464,10 +464,10 @@ Cpigemr2d(m, n, /* allocing room for the tabs, alloc for the worst case,local_n or local_m * intervals, in fact the worst case should be less, perhaps half that,I * should think of that one day. */ - h_inter = (IDESC *) mr2d_malloc(DIVUP(ma->n, q0 * ma->nbcol) * - ma->nbcol * sizeof(IDESC)); - v_inter = (IDESC *) mr2d_malloc(DIVUP(ma->m, p0 * ma->nbrow) - * ma->nbrow * sizeof(IDESC)); + h_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->n, q0 * ma->nbcol)) * + (size_t)ma->nbcol * sizeof(IDESC)); + v_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->m, p0 * ma->nbrow)) + * (size_t)ma->nbrow * sizeof(IDESC)); /* We go for the scanning of indices. For each processor including mypnum, * we fill the sendbuff buffer (scanD0(SENDBUFF)) and when it is done send * it. Then for each processor, we compute the size of message to be @@ -567,7 +567,7 @@ init_chenille(Int mypnum, Int nprocs, Int n0, Int *proc0, Int n1, Int *proc1, In Int ns, nr, i, tot; Int *sender, *recver, *g0, *g1; tot = max(n0, n1); - sender = (Int *) mr2d_malloc((nprocs + tot) * sizeof(Int) * 2); + sender = (Int *) mr2d_malloc((size_t)(nprocs + tot) * sizeof(Int) * 2); recver = sender + tot; *psend = sender; *precv = recver; @@ -703,7 +703,7 @@ gridreshape(Int *ctxtp) Int i, j; ori = *ctxtp; Cblacs_gridinfo(ori, &nprow, &npcol, &myrow, &mycol); - usermap = mr2d_malloc(sizeof(Int) * nprow * npcol); + usermap = mr2d_malloc(sizeof(Int) * (size_t)nprow * (size_t)npcol); for (i = 0; i < nprow; i++) for (j = 0; j < npcol; j++) { usermap[i + j * nprow] = Cblacs_pnum(ori, i, j); diff --git a/REDIST/SRC/pigemr2.c b/REDIST/SRC/pigemr2.c index 0e6d11d1..4d3f9f91 100644 --- a/REDIST/SRC/pigemr2.c +++ b/REDIST/SRC/pigemr2.c @@ -118,7 +118,7 @@ setmemory(Int **adpointer, Int blocksize) return; } *adpointer = (Int *) mr2d_malloc( - blocksize * sizeof(Int)); + (size_t)blocksize * sizeof(Int)); } /******************************************************************/ /* Free the memory space after the malloc */ diff --git a/REDIST/SRC/pitrmr.c b/REDIST/SRC/pitrmr.c index 043c37f9..65acaf00 100644 --- a/REDIST/SRC/pitrmr.c +++ b/REDIST/SRC/pitrmr.c @@ -356,7 +356,7 @@ Cpitrmr2d(uplo, diag, m, n, assert((myprow1 < p1 && mypcol1 < q1) || (myprow1 == -1 && mypcol1 == -1)); /* exchange the missing parameters among the processors: shape of grids and * location of the processors */ - param = (Int *) mr2d_malloc(3 * (nprocs * 2 + NBPARAM) * sizeof(Int)); + param = (Int *) mr2d_malloc(3 * ((size_t)nprocs * 2 + NBPARAM) * sizeof(Int)); ra = param + nprocs * 2 + NBPARAM; ca = param + (nprocs * 2 + NBPARAM) * 2; for (i = 0; i < nprocs * 2 + NBPARAM; i++) @@ -481,10 +481,10 @@ Cpitrmr2d(uplo, diag, m, n, /* allocing room for the tabs, alloc for the worst case,local_n or local_m * intervals, in fact the worst case should be less, perhaps half that,I * should think of that one day. */ - h_inter = (IDESC *) mr2d_malloc(DIVUP(ma->n, q0 * ma->nbcol) * - ma->nbcol * sizeof(IDESC)); - v_inter = (IDESC *) mr2d_malloc(DIVUP(ma->m, p0 * ma->nbrow) - * ma->nbrow * sizeof(IDESC)); + h_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->n, q0 * ma->nbcol)) * + (size_t)(ma->nbcol) * sizeof(IDESC)); + v_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->m, p0 * ma->nbrow)) + * (size_t)ma->nbrow * sizeof(IDESC)); /* We go for the scanning of indices. For each processor including mypnum, * we fill the sendbuff buffer (scanD0(SENDBUFF)) and when it is done send * it. Then for each processor, we compute the size of message to be @@ -589,7 +589,7 @@ init_chenille(Int mypnum, Int nprocs, Int n0, Int *proc0, Int n1, Int *proc1, In Int ns, nr, i, tot; Int *sender, *recver, *g0, *g1; tot = max(n0, n1); - sender = (Int *) mr2d_malloc((nprocs + tot) * sizeof(Int) * 2); + sender = (Int *) mr2d_malloc((size_t)(nprocs + tot) * sizeof(Int) * 2); recver = sender + tot; *psend = sender; *precv = recver; @@ -661,7 +661,7 @@ gridreshape(Int *ctxtp) Int i, j; ori = *ctxtp; Cblacs_gridinfo(ori, &nprow, &npcol, &myrow, &mycol); - usermap = mr2d_malloc(sizeof(Int) * nprow * npcol); + usermap = mr2d_malloc(sizeof(Int) * (size_t)nprow * (size_t)npcol); for (i = 0; i < nprow; i++) for (j = 0; j < npcol; j++) { usermap[i + j * nprow] = Cblacs_pnum(ori, i, j); diff --git a/REDIST/SRC/pitrmr2.c b/REDIST/SRC/pitrmr2.c index a86f207d..ac36cfc4 100644 --- a/REDIST/SRC/pitrmr2.c +++ b/REDIST/SRC/pitrmr2.c @@ -118,7 +118,7 @@ setmemory(Int **adpointer, Int blocksize) return; } *adpointer = (Int *) mr2d_malloc( - blocksize * sizeof(Int)); + (size_t)blocksize * sizeof(Int)); } /******************************************************************/ /* Free the memory space after the malloc */ diff --git a/REDIST/SRC/psgemr.c b/REDIST/SRC/psgemr.c index 6e053bfd..4e12e703 100644 --- a/REDIST/SRC/psgemr.c +++ b/REDIST/SRC/psgemr.c @@ -339,7 +339,7 @@ Cpsgemr2d(m, n, assert((myprow1 < p1 && mypcol1 < q1) || (myprow1 == -1 && mypcol1 == -1)); /* exchange the missing parameters among the processors: shape of grids and * location of the processors */ - param = (Int *) mr2d_malloc(3 * (nprocs * 2 + NBPARAM) * sizeof(Int)); + param = (Int *) mr2d_malloc(3 * ((size_t)nprocs * 2 + NBPARAM) * sizeof(Int)); ra = param + nprocs * 2 + NBPARAM; ca = param + (nprocs * 2 + NBPARAM) * 2; for (i = 0; i < nprocs * 2 + NBPARAM; i++) @@ -464,10 +464,10 @@ Cpsgemr2d(m, n, /* allocing room for the tabs, alloc for the worst case,local_n or local_m * intervals, in fact the worst case should be less, perhaps half that,I * should think of that one day. */ - h_inter = (IDESC *) mr2d_malloc(DIVUP(ma->n, q0 * ma->nbcol) * - ma->nbcol * sizeof(IDESC)); - v_inter = (IDESC *) mr2d_malloc(DIVUP(ma->m, p0 * ma->nbrow) - * ma->nbrow * sizeof(IDESC)); + h_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->n, q0 * ma->nbcol)) * + (size_t)ma->nbcol * sizeof(IDESC)); + v_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->m, p0 * ma->nbrow)) + * (size_t)ma->nbrow * sizeof(IDESC)); /* We go for the scanning of indices. For each processor including mypnum, * we fill the sendbuff buffer (scanD0(SENDBUFF)) and when it is done send * it. Then for each processor, we compute the size of message to be @@ -567,7 +567,7 @@ init_chenille(Int mypnum, Int nprocs, Int n0, Int *proc0, Int n1, Int *proc1, In Int ns, nr, i, tot; Int *sender, *recver, *g0, *g1; tot = max(n0, n1); - sender = (Int *) mr2d_malloc((nprocs + tot) * sizeof(Int) * 2); + sender = (Int *) mr2d_malloc((size_t)(nprocs + tot) * sizeof(Int) * 2); recver = sender + tot; *psend = sender; *precv = recver; @@ -703,7 +703,7 @@ gridreshape(Int *ctxtp) Int i, j; ori = *ctxtp; Cblacs_gridinfo(ori, &nprow, &npcol, &myrow, &mycol); - usermap = mr2d_malloc(sizeof(Int) * nprow * npcol); + usermap = mr2d_malloc(sizeof(Int) * (size_t)nprow * (size_t)npcol); for (i = 0; i < nprow; i++) for (j = 0; j < npcol; j++) { usermap[i + j * nprow] = Cblacs_pnum(ori, i, j); diff --git a/REDIST/SRC/psgemr2.c b/REDIST/SRC/psgemr2.c index 07b35687..55442db9 100644 --- a/REDIST/SRC/psgemr2.c +++ b/REDIST/SRC/psgemr2.c @@ -118,7 +118,7 @@ setmemory(float **adpointer, Int blocksize) return; } *adpointer = (float *) mr2d_malloc( - blocksize * sizeof(float)); + (size_t)blocksize * sizeof(float)); } /******************************************************************/ /* Free the memory space after the malloc */ diff --git a/REDIST/SRC/pstrmr.c b/REDIST/SRC/pstrmr.c index e63b93fd..89ed468b 100644 --- a/REDIST/SRC/pstrmr.c +++ b/REDIST/SRC/pstrmr.c @@ -350,7 +350,7 @@ Cpstrmr2d(char *uplo, char *diag, Int m, Int n, assert((myprow1 < p1 && mypcol1 < q1) || (myprow1 == -1 && mypcol1 == -1)); /* exchange the missing parameters among the processors: shape of grids and * location of the processors */ - param = (Int *) mr2d_malloc(3 * (nprocs * 2 + NBPARAM) * sizeof(Int)); + param = (Int *) mr2d_malloc(3 * ((size_t)nprocs * 2 + NBPARAM) * sizeof(Int)); ra = param + nprocs * 2 + NBPARAM; ca = param + (nprocs * 2 + NBPARAM) * 2; for (i = 0; i < nprocs * 2 + NBPARAM; i++) @@ -475,10 +475,10 @@ Cpstrmr2d(char *uplo, char *diag, Int m, Int n, /* allocing room for the tabs, alloc for the worst case,local_n or local_m * intervals, in fact the worst case should be less, perhaps half that,I * should think of that one day. */ - h_inter = (IDESC *) mr2d_malloc(DIVUP(ma->n, q0 * ma->nbcol) * - ma->nbcol * sizeof(IDESC)); - v_inter = (IDESC *) mr2d_malloc(DIVUP(ma->m, p0 * ma->nbrow) - * ma->nbrow * sizeof(IDESC)); + h_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->n, q0 * ma->nbcol)) * + (size_t)ma->nbcol * sizeof(IDESC)); + v_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->m, p0 * ma->nbrow)) + * (size_t)ma->nbrow * sizeof(IDESC)); /* We go for the scanning of indices. For each processor including mypnum, * we fill the sendbuff buffer (scanD0(SENDBUFF)) and when it is done send * it. Then for each processor, we compute the size of message to be @@ -583,7 +583,7 @@ init_chenille(Int mypnum, Int nprocs, Int n0, Int *proc0, Int n1, Int *proc1, In Int ns, nr, i, tot; Int *sender, *recver, *g0, *g1; tot = max(n0, n1); - sender = (Int *) mr2d_malloc((nprocs + tot) * sizeof(Int) * 2); + sender = (Int *) mr2d_malloc((size_t)(nprocs + tot) * sizeof(Int) * 2); recver = sender + tot; *psend = sender; *precv = recver; @@ -655,7 +655,7 @@ gridreshape(Int *ctxtp) Int i, j; ori = *ctxtp; Cblacs_gridinfo(ori, &nprow, &npcol, &myrow, &mycol); - usermap = mr2d_malloc(sizeof(Int) * nprow * npcol); + usermap = mr2d_malloc(sizeof(Int) * (size_t)nprow * (size_t)npcol); for (i = 0; i < nprow; i++) for (j = 0; j < npcol; j++) { usermap[i + j * nprow] = Cblacs_pnum(ori, i, j); diff --git a/REDIST/SRC/pstrmr2.c b/REDIST/SRC/pstrmr2.c index dcec2b5b..7b59761c 100644 --- a/REDIST/SRC/pstrmr2.c +++ b/REDIST/SRC/pstrmr2.c @@ -118,7 +118,7 @@ setmemory(float **adpointer, Int blocksize) return; } *adpointer = (float *) mr2d_malloc( - blocksize * sizeof(float)); + (size_t)blocksize * sizeof(float)); } /******************************************************************/ /* Free the memory space after the malloc */ diff --git a/REDIST/SRC/pzgemr.c b/REDIST/SRC/pzgemr.c index 4b2f014b..60475990 100644 --- a/REDIST/SRC/pzgemr.c +++ b/REDIST/SRC/pzgemr.c @@ -342,7 +342,7 @@ Cpzgemr2d(m, n, assert((myprow1 < p1 && mypcol1 < q1) || (myprow1 == -1 && mypcol1 == -1)); /* exchange the missing parameters among the processors: shape of grids and * location of the processors */ - param = (Int *) mr2d_malloc(3 * (nprocs * 2 + NBPARAM) * sizeof(Int)); + param = (Int *) mr2d_malloc(3 * ((size_t)nprocs * 2 + NBPARAM) * sizeof(Int)); ra = param + nprocs * 2 + NBPARAM; ca = param + (nprocs * 2 + NBPARAM) * 2; for (i = 0; i < nprocs * 2 + NBPARAM; i++) @@ -467,10 +467,10 @@ Cpzgemr2d(m, n, /* allocing room for the tabs, alloc for the worst case,local_n or local_m * intervals, in fact the worst case should be less, perhaps half that,I * should think of that one day. */ - h_inter = (IDESC *) mr2d_malloc(DIVUP(ma->n, q0 * ma->nbcol) * - ma->nbcol * sizeof(IDESC)); - v_inter = (IDESC *) mr2d_malloc(DIVUP(ma->m, p0 * ma->nbrow) - * ma->nbrow * sizeof(IDESC)); + h_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->n, q0 * ma->nbcol)) * + (size_t)ma->nbcol * sizeof(IDESC)); + v_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->m, p0 * ma->nbrow)) + * (size_t)ma->nbrow * sizeof(IDESC)); /* We go for the scanning of indices. For each processor including mypnum, * we fill the sendbuff buffer (scanD0(SENDBUFF)) and when it is done send * it. Then for each processor, we compute the size of message to be @@ -570,7 +570,7 @@ init_chenille(Int mypnum, Int nprocs, Int n0, Int *proc0, Int n1, Int *proc1, In Int ns, nr, i, tot; Int *sender, *recver, *g0, *g1; tot = max(n0, n1); - sender = (Int *) mr2d_malloc((nprocs + tot) * sizeof(Int) * 2); + sender = (Int *) mr2d_malloc((size_t)(nprocs + tot) * sizeof(Int) * 2); recver = sender + tot; *psend = sender; *precv = recver; @@ -706,7 +706,7 @@ gridreshape(Int *ctxtp) Int i, j; ori = *ctxtp; Cblacs_gridinfo(ori, &nprow, &npcol, &myrow, &mycol); - usermap = mr2d_malloc(sizeof(Int) * nprow * npcol); + usermap = mr2d_malloc(sizeof(Int) * (size_t)nprow * (size_t)npcol); for (i = 0; i < nprow; i++) for (j = 0; j < npcol; j++) { usermap[i + j * nprow] = Cblacs_pnum(ori, i, j); diff --git a/REDIST/SRC/pzgemr2.c b/REDIST/SRC/pzgemr2.c index 979748e1..862fdfff 100644 --- a/REDIST/SRC/pzgemr2.c +++ b/REDIST/SRC/pzgemr2.c @@ -121,7 +121,7 @@ setmemory(dcomplex **adpointer, Int blocksize) return; } *adpointer = (dcomplex *) mr2d_malloc( - blocksize * sizeof(dcomplex)); + (size_t)blocksize * sizeof(dcomplex)); } /******************************************************************/ /* Free the memory space after the malloc */ diff --git a/REDIST/SRC/pztrmr.c b/REDIST/SRC/pztrmr.c index 2de759a4..a0c18d89 100644 --- a/REDIST/SRC/pztrmr.c +++ b/REDIST/SRC/pztrmr.c @@ -341,6 +341,7 @@ Cpztrmr2d(uplo, diag, m, n, Cblacs_gridinfo(globcontext, &nprow, &npcol, &dummy, &mypnum); gcontext = globcontext; nprocs = nprow * npcol; + assert (nprocs > 0); /* if the global context that is given to us has not the shape of a line * (nprow != 1), create a new context. TODO: to be optimal, we should * avoid this because it is an uncessary synchronisation */ @@ -359,7 +360,7 @@ Cpztrmr2d(uplo, diag, m, n, assert((myprow1 < p1 && mypcol1 < q1) || (myprow1 == -1 && mypcol1 == -1)); /* exchange the missing parameters among the processors: shape of grids and * location of the processors */ - param = (Int *) mr2d_malloc(3 * (nprocs * 2 + NBPARAM) * sizeof(Int)); + param = (Int *) mr2d_malloc(3 * ((size_t)nprocs * 2 + NBPARAM) * sizeof(Int)); ra = param + nprocs * 2 + NBPARAM; ca = param + (nprocs * 2 + NBPARAM) * 2; for (i = 0; i < nprocs * 2 + NBPARAM; i++) @@ -484,10 +485,10 @@ Cpztrmr2d(uplo, diag, m, n, /* allocing room for the tabs, alloc for the worst case,local_n or local_m * intervals, in fact the worst case should be less, perhaps half that,I * should think of that one day. */ - h_inter = (IDESC *) mr2d_malloc(DIVUP(ma->n, q0 * ma->nbcol) * - ma->nbcol * sizeof(IDESC)); - v_inter = (IDESC *) mr2d_malloc(DIVUP(ma->m, p0 * ma->nbrow) - * ma->nbrow * sizeof(IDESC)); + h_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->n, q0 * ma->nbcol)) * + (size_t)ma->nbcol * sizeof(IDESC)); + v_inter = (IDESC *) mr2d_malloc((size_t)(DIVUP(ma->m, p0 * ma->nbrow)) + * (size_t)ma->nbrow * sizeof(IDESC)); /* We go for the scanning of indices. For each processor including mypnum, * we fill the sendbuff buffer (scanD0(SENDBUFF)) and when it is done send * it. Then for each processor, we compute the size of message to be @@ -592,7 +593,7 @@ init_chenille(Int mypnum, Int nprocs, Int n0, Int *proc0, Int n1, Int *proc1, In Int ns, nr, i, tot; Int *sender, *recver, *g0, *g1; tot = max(n0, n1); - sender = (Int *) mr2d_malloc((nprocs + tot) * sizeof(Int) * 2); + sender = (Int *) mr2d_malloc((size_t)(nprocs + tot) * sizeof(Int) * 2); recver = sender + tot; *psend = sender; *precv = recver; @@ -664,7 +665,7 @@ gridreshape(Int *ctxtp) Int i, j; ori = *ctxtp; Cblacs_gridinfo(ori, &nprow, &npcol, &myrow, &mycol); - usermap = mr2d_malloc(sizeof(Int) * nprow * npcol); + usermap = mr2d_malloc(sizeof(Int) * (size_t)nprow * (size_t)npcol); for (i = 0; i < nprow; i++) for (j = 0; j < npcol; j++) { usermap[i + j * nprow] = Cblacs_pnum(ori, i, j); diff --git a/REDIST/SRC/pztrmr2.c b/REDIST/SRC/pztrmr2.c index c75abce5..29f22901 100644 --- a/REDIST/SRC/pztrmr2.c +++ b/REDIST/SRC/pztrmr2.c @@ -121,7 +121,7 @@ setmemory(dcomplex **adpointer, Int blocksize) return; } *adpointer = (dcomplex *) mr2d_malloc( - blocksize * sizeof(dcomplex)); + (size_t) blocksize * sizeof(dcomplex)); } /******************************************************************/ /* Free the memory space after the malloc */ From eaabe787c96e5e884be2e83c7a56e7540e0acc44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Donval?= Date: Wed, 4 Oct 2023 11:40:27 +0100 Subject: [PATCH 4/4] Fixes negative value detection --- REDIST/SRC/pgemraux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/REDIST/SRC/pgemraux.c b/REDIST/SRC/pgemraux.c index e078e07e..404af7a1 100644 --- a/REDIST/SRC/pgemraux.c +++ b/REDIST/SRC/pgemraux.c @@ -104,7 +104,7 @@ extern void Cpigemr2d(); #include #include #include -const size_t NEGFLAG = -1; +const size_t NEGFLAG = ~( ((size_t)-1) >> 1); void * mr2d_malloc(size_t n) {