Skip to content

Commit

Permalink
Revert "use get2x2 in quasiswap"
Browse files Browse the repository at this point in the history
This reverts commit 23a5e30.

Tests showed that the new 2x2 scheme was usually slower or only
marginally faster (<1%) than the previous 3minus scheme both in
Linux i7 desktop and Macbook Air i5 laptop, and therefore we
revert to the previous 3minus scheme. This is strange, because
profiling compiled code with operf hinted that 2x2 should be faster
except for sparse data (bryceveg). Here microbenchmarks from Linux:

> mb2x2
Unit: milliseconds
                                          expr  min   lq mean median   uq  max neval
    simulate(nullmodel(sipoo,"quasiswap"),100)  402  442  462    464  480  537   100
      simulate(nullmodel(BCI,"quasiswap"),100) 6803 7461 7659   7726 7915 8483   100
 simulate(nullmodel(bryceveg,"quasiswap"),100) 3301 3550 3682   3674 3805 4059   100
> mb3minus
Unit: milliseconds
                                          expr  min   lq mean median   uq  max neval
    simulate(nullmodel(sipoo,"quasiswap"),100)  358  404  422    422  440  642   100
      simulate(nullmodel(BCI,"quasiswap"),100) 7101 7517 7689   7686 7830 8382   100
 simulate(nullmodel(bryceveg,"quasiswap"),100) 3007 3088 3137   3124 3168 3462   100
  • Loading branch information
Jari Oksanen committed Oct 11, 2017
1 parent 23a5e30 commit 72b2d93
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/nestedness.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static void get2x2(int len, int nr, int *acbd)

static void quasiswap(int *m, int *nr, int *nc, int *thin)
{
int i, n, mtot, ss, acbd[4], n1, nr1, nc1, a, b, c, d;
int i, n, mtot, ss, row[2], col[2], n1, nr1, nc1, a, b, c, d;
size_t intcheck;

nr1 = (*nr) - 1;
Expand All @@ -101,13 +101,22 @@ static void quasiswap(int *m, int *nr, int *nc, int *thin)
intcheck = 0; /* check interrupts */
while (ss > mtot) {
for (i = 0; i < *thin; i++) {
/* get a 2x2 matrix and its incides */
get2x2(n1, *nr, acbd);
a = acbd[0];
c = acbd[1];
b = acbd[2];
d = acbd[3];
/* quasiswap when you can */
/* first item and its row & column indices */
a = IRAND(n1);
row[0] = a % (*nr);
col[0] = a / (*nr);
/* neighbour from the same col but different row */
do {row[1] = IRAND(nr1);} while (row[1] == row[0]);
c = INDX(row[1], col[0], *nr);
/* if neighbours are both zero, cannot be quasiswapped
(probability (1-f)^2 with relative col fill f) */
if (m[c] == 0 && m[a] == 0)
continue;
/* second col */
do {col[1] = IRAND(nc1);} while (col[1] == col[0]);
b = INDX(row[0], col[1], *nr);
d = INDX(row[1], col[1], *nr);
/* m[a] has 50% chance of being > 0, m[d] less, so have it first */
if (m[d] > 0 && m[a] > 0 && m[a] + m[d] - m[b] - m[c] >= 2) {
ss -= 2 * (m[a] + m[d] - m[b] - m[c] - 2);
m[a]--;
Expand Down

0 comments on commit 72b2d93

Please sign in to comment.