Example #1
0
function f()
{
    global $_tpl;
    #$n = intval(($argc == 2) ? $argv[1] : 400);
    $n = 400;
    $u = array_fill(0, $n, 1.0);
    $_tpl = array_fill(0, $n, 0.0);
    for ($i = 0; $i < 10; $i++) {
        $v = AtAv($n, $u);
        $u = AtAv($n, $v);
    }
    $vBv = 0.0;
    $vv = 0.0;
    $i = 0;
    foreach ($v as $val) {
        $vBv += $u[$i] * $val;
        $vv += $val * $val;
        ++$i;
    }
    sqrt($vBv / $vv);
}
Example #2
0
            if ($pid) {
                continue;
            }
        }
        break;
    } else {
        $range_end = $n;
        $parent = TRUE;
    }
}
$u = array_fill(0, $n, 1.0);
$_tpl = array_fill($range_begin, $range_end - $range_begin, 0.0);
$sync = $procs > 0;
for ($i = 0; $i < 10; $i++) {
    $v = AtAv($n, $u, $range_begin, $range_end, $sync);
    $u = AtAv($n, $v, $range_begin, $range_end, $sync);
}
if (!$parent) {
    exit(0);
}
$childs = $procs - 1;
while ($childs--) {
    pcntl_wait($s);
}
$vBv = 0.0;
$vv = 0.0;
$i = 0;
foreach ($v as $val) {
    $vBv += $u[$i] * $val;
    $vv += $val * $val;
    ++$i;
    $Atv = $_tpl;
    for ($i = 0; $i < $n; ++$i) {
        $sum = 0.0;
        foreach ($v as $j => $v_j) {
            $sum += A($j, $i) * $v_j;
        }
        $Atv[$i] = $sum;
    }
    return $Atv;
}
function AtAv(&$n, &$v)
{
    $tmp = Av($n, $v);
    return Atv($n, $tmp);
}
$n = intval($argc == 2 ? $argv[1] : 1);
$u = array_fill(0, $n, 1.0);
$_tpl = array_fill(0, $n, 0.0);
for ($i = 0; $i < 10; $i++) {
    $v = AtAv($n, $u);
    $u = AtAv($n, $v);
}
$vBv = 0.0;
$vv = 0.0;
$i = 0;
foreach ($v as $val) {
    $vBv += $u[$i] * $val;
    $vv += $val * $val;
    ++$i;
}
printf("%0.9f\n", sqrt($vBv / $vv));
Example #4
0
function spectral_norm($n = 200)
{
    $u = array_pad(array(), $n, 1);
    for ($i = 0; $i < 10; $i++) {
        $v = AtAv($n, $u);
        $u = AtAv($n, $v);
    }
    $vBv = 0;
    $vv = 0;
    for ($i = 0; $i < $n; $i++) {
        $vBv += $u[$i] * $v[$i];
        $vv += $v[$i] * $v[$i];
    }
    printf("%0.9f\n", sqrt($vBv / $vv));
}
Example #5
0
 private static function spectralnorm($n)
 {
     //$n = (int) (($argc == 2) ? $argv[1] : 1);
     $procs = 1;
     if (file_exists('/proc/cpuinfo')) {
         $procs = preg_match_all('/^processor\\s/m', file_get_contents('/proc/cpuinfo'), $discard);
     }
     if ($n < $procs) {
         $procs = 1;
     }
     $chunk_size = (int) ($n / $procs);
     $double_size = strlen(pack('d', 0.0));
     $chunk_data_size = $double_size * $chunk_size;
     $total_data_size = $double_size * $n;
     $pipes = array();
     $parent = FALSE;
     for ($i = 0; $i < $procs; ++$i) {
         $range_begin = $i * $chunk_size;
         if ($i < $procs - 1) {
             $pipe = pipe();
             $pipes[] = $pipe[0];
             $pipe = $pipe[1];
             $range_end = $range_begin + $chunk_size;
             $pid = pcntl_fork();
             if ($pid === -1) {
                 die('could not fork');
             } else {
                 if ($pid) {
                     continue;
                 }
             }
             break;
         } else {
             $range_end = $n;
             $parent = TRUE;
         }
     }
     $u = array_fill(0, $n, 1.0);
     $_tpl = array_fill($range_begin, $range_end - $range_begin, 0.0);
     $sync = $procs > 0;
     for ($i = 0; $i < 10; $i++) {
         $v = AtAv($n, $u, $range_begin, $range_end, $sync);
         $u = AtAv($n, $v, $range_begin, $range_end, $sync);
     }
     if (!$parent) {
         exit(0);
     }
     $childs = $procs - 1;
     while ($childs--) {
         pcntl_wait($s);
     }
     $vBv = 0.0;
     $vv = 0.0;
     $i = 0;
     foreach ($v as $val) {
         $vBv += $u[$i] * $val;
         $vv += $val * $val;
         ++$i;
     }
     printf("%0.9f\n", sqrt($vBv / $vv));
 }