From manuel.lopez-ibanez at ulb.ac.be  Wed Feb 10 14:45:11 2010
From: manuel.lopez-ibanez at ulb.ac.be (manuel.lopez-ibanez at ulb.ac.be)
Date: Wed, 10 Feb 2010 14:45:11 +0100 (CET)
Subject: [Rd] system.time provides inaccurate sys.child (PR#14210)
Message-ID: <20100210134511.4BDA32830315@mail.pubhealth.ku.dk>

Full_Name: Manuel L?pez-Ib??ez
Version: R version 2.6.2 (2008-02-08)
OS: linux-gnu 
Submission from: (NULL) (164.15.10.156)


This is only relevant for CPU intensive child processes. Otherwise, the problem
is not obvious.

Therefore, we need a CPU intensive program like this one:

/************************************/
/*** Compile with: gcc -o timer-test -O0 timer-test.c -lm */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double alpha, beta;
int size = 1000;

#define WORK_create_matrix(TYPEOFMATRIX)                \
                                                        \
TYPEOFMATRIX ** m_create_##TYPEOFMATRIX##_matrix (      \
int dim1, int dim2, int line, const char*file)          \
{                                                       \
    TYPEOFMATRIX **p;                                   \
    int i;                                              \
                                                        \
    p = malloc (sizeof(TYPEOFMATRIX) * dim1 * dim2      \
                + sizeof(TYPEOFMATRIX *) * dim1 );      \
    if (p == NULL) {                                    \
        fprintf(stderr, "cannot create " #TYPEOFMATRIX  \
                " matrix of size (%d x %d): "           \
                "see line %d of file %s\n",             \
                dim1, dim2, line, file);                \
        exit(1);                                        \
    }                                                   \
    for (i = 0; i < dim1; i++)                          \
        p[i] = (TYPEOFMATRIX *) (p + dim1) + i * dim2;  \
    return p;                                           \
}                                                       

WORK_create_matrix(int)
WORK_create_matrix(double)
#undef WORK_create_matrix
#define create_double_matrix(dim1,dim2)\
    m_create_double_matrix(dim1,dim2,__LINE__,__FILE__)


int main(int argc, char *argv[])
{
    double **matrix1 = create_double_matrix(size, size);
    double **matrix2 = create_double_matrix(size, size);
    int iterations = 0;
    int i,j;
    double iter_limit = 100;
    alpha = rand();
    beta = rand();

    while (iterations < iter_limit) {
        for (i = 0; i < size; i++) {
            for (j = 0; j < size; j++) {
                if (i == j) continue;
                matrix2[i][j] = pow(matrix1[i][j], alpha)
                    * pow(matrix2[j][i], beta);
                matrix1[j][i] = matrix2[i][j];
            }
        }
        iterations++;
    }

    printf("Iterations = %d\n", iterations);
    return 0;
}
/************************************/

Then in R evaluate:

> print.default (system.time (system(paste ("time", "bash -c './timer-test 100 >
/dev/null'"))))
10.77user 0.02system 0:10.81elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+4574minor)pagefaults 0swaps
 user.self   sys.self    elapsed user.child  sys.child 
     0.000      0.000     10.818     10.777     10.777 
attr(,"class")
[1] "proc_time"

Expected: the sys.child time should be 0.02.

> version
               _                           
platform       i486-pc-linux-gnu           
arch           i486                        
os             linux-gnu                   
system         i486, linux-gnu             
status                                     
major          2                           
minor          6.2                         
year           2008                        
month          02                          
day            08                          
svn rev        44383                       
language       R                           
version.string R version 2.6.2 (2008-02-08)



[Rd] system.time provides inaccurate sys.child (PR#14210)
manuel.lopez-ibanez at ulb.ac.be