Home | History | Annotate | Download | only in shootout
      1 /*
      2 Redistribution and use in source and binary forms, with or without
      3 modification, are permitted provided that the following conditions are met:
      4 
      5     * Redistributions of source code must retain the above copyright
      6     notice, this list of conditions and the following disclaimer.
      7 
      8     * Redistributions in binary form must reproduce the above copyright
      9     notice, this list of conditions and the following disclaimer in the
     10     documentation and/or other materials provided with the distribution.
     11 
     12     * Neither the name of "The Computer Language Benchmarks Game" nor the
     13     name of "The Computer Language Shootout Benchmarks" nor the names of
     14     its contributors may be used to endorse or promote products derived
     15     from this software without specific prior written permission.
     16 
     17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27 POSSIBILITY OF SUCH DAMAGE.
     28 */
     29 
     30 /* The Computer Language Benchmarks Game
     31  * http://shootout.alioth.debian.org/
     32  *
     33  * contributed by The Go Authors.
     34  * Based on spectral-norm.c by Sebastien Loisel
     35  */
     36 
     37 package main
     38 
     39 import (
     40 	"flag"
     41 	"fmt"
     42 	"math"
     43 	"runtime"
     44 )
     45 
     46 var n = flag.Int("n", 2000, "count")
     47 var nCPU = flag.Int("ncpu", 4, "number of cpus")
     48 
     49 func evalA(i, j int) float64 { return 1 / float64(((i+j)*(i+j+1)/2 + i + 1)) }
     50 
     51 type Vec []float64
     52 
     53 func (v Vec) Times(i, n int, u Vec, c chan int) {
     54 	for ; i < n; i++ {
     55 		v[i] = 0
     56 		for j := 0; j < len(u); j++ {
     57 			v[i] += evalA(i, j) * u[j]
     58 		}
     59 	}
     60 	c <- 1
     61 }
     62 
     63 func (v Vec) TimesTransp(i, n int, u Vec, c chan int) {
     64 	for ; i < n; i++ {
     65 		v[i] = 0
     66 		for j := 0; j < len(u); j++ {
     67 			v[i] += evalA(j, i) * u[j]
     68 		}
     69 	}
     70 	c <- 1
     71 }
     72 
     73 func wait(c chan int) {
     74 	for i := 0; i < *nCPU; i++ {
     75 		<-c
     76 	}
     77 }
     78 
     79 func (v Vec) ATimesTransp(u Vec) {
     80 	x := make(Vec, len(u))
     81 	c := make(chan int, *nCPU)
     82 	for i := 0; i < *nCPU; i++ {
     83 		go x.Times(i*len(v) / *nCPU, (i+1)*len(v) / *nCPU, u, c)
     84 	}
     85 	wait(c)
     86 	for i := 0; i < *nCPU; i++ {
     87 		go v.TimesTransp(i*len(v) / *nCPU, (i+1)*len(v) / *nCPU, x, c)
     88 	}
     89 	wait(c)
     90 }
     91 
     92 func main() {
     93 	flag.Parse()
     94 	runtime.GOMAXPROCS(*nCPU)
     95 	N := *n
     96 	u := make(Vec, N)
     97 	for i := 0; i < N; i++ {
     98 		u[i] = 1
     99 	}
    100 	v := make(Vec, N)
    101 	for i := 0; i < 10; i++ {
    102 		v.ATimesTransp(u)
    103 		u.ATimesTransp(v)
    104 	}
    105 	var vBv, vv float64
    106 	for i := 0; i < N; i++ {
    107 		vBv += u[i] * v[i]
    108 		vv += v[i] * v[i]
    109 	}
    110 	fmt.Printf("%0.9f\n", math.Sqrt(vBv/vv))
    111 }
    112