Commit 881edf4e authored by igouy-guest's avatar igouy-guest

cvsimport

parents 3a04f7d6 09267239
/* The Computer Language Benchmarks Game
* http://benchmarksgame.alioth.debian.org/
*
* Contributed by Martin Koistinen
* Based on mandelbrot.c contributed by Greg Buchholz and The Go Authors
* flag.Arg hack by Isaac Gouy
*
* Large changes by Bill Broadley, including:
* 1) Switching the one goroutine per line to one per CPU
* 2) Replacing gorouting calls with channels
* 3) Handling out of order results in the file writer.
* modified by Sean Lake
*
* modified by Rodrigo Corsi
* 1)two goroutines per cpu
* 2)each goroutine generate one line and increment counter (atomic int32)
* modified by Anton Yuzhaninov
*
*/
package main
import (
"bufio"
"flag"
"fmt"
"os"
"runtime"
"strconv"
"sync"
"sync/atomic"
)
const limit = 4.0 // abs(z) < 2
const maxIter = 50 // number of iterations
const defaultSize = 4000 // bitmap size if not given as command-line argument
var rows [][]byte
var bytesPerRow int
var initial_r []float64
var initial_i []float64
// renderRow returns rendered row of pixels
// pixels packed in one byte for PBM image
func renderRow(y0 *int32) []byte {
var i, j, x int
var res, b byte
var Zr1, Zr2, Zi1, Zi2, Tr1, Tr2, Ti1, Ti2 float64
row := make([]byte, bytesPerRow)
for xByte := range row {
res = 0
Ci := initial_i[*y0]
for i = 0; i < 8; i += 2 {
x = xByte << 3 // * 8
Cr1 := initial_r[x+i]
Cr2 := initial_r[x+i+1]
Zr1 = Cr1
Zi1 = Ci
Zr2 = Cr2
Zi2 = Ci
b = 0
for j = 0; j < maxIter; j++ {
Tr1 = Zr1 * Zr1
Ti1 = Zi1 * Zi1
Zi1 = 2*Zr1*Zi1 + Ci
Zr1 = Tr1 - Ti1 + Cr1
if Tr1+Ti1 > limit {
b |= 2
if b == 3 {
break
}
}
Tr2 = Zr2 * Zr2
Ti2 = Zi2 * Zi2
Zi2 = 2*Zr2*Zi2 + Ci
Zr2 = Tr2 - Ti2 + Cr2
if Tr2+Ti2 > limit {
b |= 1
if b == 3 {
break
}
}
}
res = (res << 2) | b
}
row[xByte] = ^res
}
return row
}
var yAt int32 = -1
func renderRows(wg *sync.WaitGroup, s32 int32) {
var y int32
for y = atomic.AddInt32(&yAt, 1); y < s32; y = atomic.AddInt32(&yAt, 1) {
rows[y] = renderRow(&y)
}
wg.Done()
}
func main() {
pool := runtime.NumCPU() * 2
runtime.GOMAXPROCS(pool)
// Get input, if any...
size := defaultSize
flag.Parse()
if flag.NArg() > 0 {
size, _ = strconv.Atoi(flag.Arg(0))
}
bytesPerRow = size >> 3
// Precompute the initial real and imaginary values for each x and y
// coordinate in the image.
initial_r = make([]float64, size)
initial_i = make([]float64, size)
inv := 2.0 / float64(size)
for xy := 0; xy < size; xy++ {
i := inv * float64(xy)
initial_r[xy] = i - 1.5
initial_i[xy] = i - 1.0
}
rows = make([][]byte, size)
/* Wait group for finish */
wg := new(sync.WaitGroup)
wg.Add(pool)
// start pool workers, and assign all work
for i := 0; i < pool; i++ {
go renderRows(wg, int32(size))
}
/* wait for the file workers to finish, then write */
wg.Wait()
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
fmt.Fprintf(out, "P4\n%d %d\n", size, size)
for y := 0; y < size; y++ {
out.Write(rows[y])
}
}
! The Computer Language Benchmarks Game
! http://benchmarksgame.alioth.debian.org/
!
! contributed by Pascal Parois
program mandelbrot
implicit none
integer, parameter :: iter = 50, vsize=48
integer, parameter :: int8 = selected_int_kind(2)
integer, parameter :: dp = selected_real_kind(15, 307)
integer :: i, j,k,l, pos
real(dp),dimension(vsize) :: cr,ci,zr,zi,tr,ti
real(dp) cte
integer :: bytepos, grid
integer(int8) :: byte
character(len=:), dimension(:),allocatable :: buf ! character buffer to avoid integer formatting on output
integer w
character(len=8) :: argv
character(len=100) :: sbuffer, formatstr
real(dp), parameter :: threshold = 4.0_dp
call get_command_argument(1, argv)
read(argv, *) w
grid=ceiling(w/real(vsize))*vsize
allocate(character(len=grid/8) :: buf(w))
buf=''
cte=2.0_dp/real(w,dp)
!$omp parallel do default(none) shared(grid, buf, cte, w)&
!$omp& private(i, j, k, bytepos, pos, byte)&
!$omp& private(zr, zi, cr, ci, tr, ti)&
!$omp& schedule(guided)
do i=0, w-1
pos=0
byte=0_int8
bytepos=8
do j = 0, grid-1,vsize
ci = cte*real(i,dp)-1.0_dp
do k=0, vsize-1
cr(k+1) = cte*real(j+k,dp)-1.5_dp
end do
zr=0.0_dp
zi=0.0_dp
tr=0.0_dp
ti=0.0_dp
do k=1, iter
zi=2.0_dp*zr*zi+ci
zr=tr-ti+cr
ti=zi*zi
tr=zr*zr
if (all(tr+ti>threshold)) then
exit
end if
end do
do k=1, vsize
bytepos=bytepos-1
if (.not. isnan(tr(k)+ti(k)) .and. tr(k)+ti(k)<threshold .and. j<=w) then
byte = ibset(byte, bytepos)
end if
if(bytepos==0) then
bytepos=8
pos = pos + 1
buf(i+1)(pos:pos) = transfer(byte, 'a')
byte=0_int8
end if
end do
end do
end do
!$omp end parallel do
! pbm header
write(*,'("P4",/,i0," ",i0)') w, w
! print output
write(formatstr, '(A,I0,A)') '(A',ceiling(w/8.0),')'
do i = 1, w
write(*, formatstr, advance='no') buf(i)(1:ceiling(w/8.0))
end do
end program mandelbrot
// The Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org/
//
// Contributed by Kevin Miller
// Converted from C to Rust by Tung Duong
extern crate rayon;
use std::ops::{Add, Sub, Mul};
use std::io::Write;
use rayon::prelude::*;
#[allow(non_camel_case_types)]
#[derive(Clone, Copy)]
struct f64x2(f64,f64);
const ZEROS: [f64x2;4] = [f64x2(0.0,0.0); 4];
impl Add for f64x2 {
type Output = Self;
fn add(self, rhs: Self) -> Self {
f64x2(self.0 + rhs.0, self.1 + rhs.1)
}
}
impl Sub for f64x2 {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
f64x2(self.0 - rhs.0, self.1 - rhs.1)
}
}
impl Mul for f64x2 {
type Output = Self;
fn mul(self, rhs: Self) -> Self {
f64x2(self.0 * rhs.0, self.1 * rhs.1)
}
}
#[inline(always)]
fn vec_nle(v : &[f64x2;4], f : f64) -> bool {
return if v[0].0 <= f ||
v[0].1 <= f ||
v[1].0 <= f ||
v[1].1 <= f ||
v[2].0 <= f ||
v[2].1 <= f ||
v[3].0 <= f ||
v[3].1 <= f {false} else {true};
}
#[inline(always)]
fn clr_pixels_nle(v : &[f64x2;4], f: f64, pix8 : &mut u8){
if !(v[0].0 <= f) {*pix8 &= 0x7f;}
if !(v[0].1 <= f) {*pix8 &= 0xbf;}
if !(v[1].0 <= f) {*pix8 &= 0xdf;}
if !(v[1].1 <= f) {*pix8 &= 0xef;}
if !(v[2].0 <= f) {*pix8 &= 0xf7;}
if !(v[2].1 <= f) {*pix8 &= 0xfb;}
if !(v[3].0 <= f) {*pix8 &= 0xfd;}
if !(v[3].1 <= f) {*pix8 &= 0xfe;}
}
#[inline(always)]
fn calc_sum(r: &mut [f64x2;4], i : &mut[f64x2;4], sum : &mut[f64x2;4], init_r: &[f64x2;4], init_i : f64x2){
for j in 0..4{
let r2 = r[j] * r[j];
let i2 = i[j] * i[j];
let ri = r[j] * i[j];
sum[j] = r2 + i2;
r[j] = r2 - i2 + init_r[j];
i[j] = ri + ri + init_i;
}
}
#[inline(always)]
fn mand8(init_r: &[f64x2;4], init_i : f64x2) -> u8 {
let mut r = ZEROS;
let mut i = ZEROS;
let mut sum = ZEROS;
for j in 0..4{
r[j] = init_r[j];
i[j] = init_i;
}
let mut pix8 : u8 = 0xff;
for _ in 0..6 {
for _ in 0..8{
calc_sum(&mut r, &mut i, &mut sum, &init_r, init_i);
}
if vec_nle(&sum, 4.0) {
pix8 = 0x00;
break;
}
}
if pix8 != 0 {
calc_sum(&mut r, &mut i, &mut sum, &init_r, init_i);
calc_sum(&mut r, &mut i, &mut sum, &init_r, init_i);
clr_pixels_nle(&sum, 4.0, &mut pix8);
}
pix8
}
fn mand64(init_r: &[f64x2;32], init_i : f64x2, out : &mut [u8]) {
let mut tmp_init_r = ZEROS;
for i in 0..8 {
tmp_init_r.copy_from_slice(&init_r[4*i..4*i+4]);
out[i] = mand8(&tmp_init_r, init_i);
}
}
fn main(){
let mut width = std::env::args_os().nth(1)
.and_then(|s| s.into_string().ok())
.and_then(|n| n.parse().ok())
.unwrap_or(16000);
width = (width+7) & !7;
println!("P4\n{} {}", width, width);
let mut r0 = vec![f64x2(0.0,0.0); width/2];
let mut i0 = vec![0.0; width];
for i in 0..width/2 {
let x1 = (2*i) as f64;
let x2 = (2*i+1) as f64;
let k = 2.0 / (width as f64);
r0[i] = f64x2(k*x1, k*x2) - f64x2(1.5,1.5);
i0[2*i] = k*x1 - 1.0;
i0[2*i+1] = k*x2 - 1.0;
}
let rows : Vec<_> = if width%64==0 {
// process 64 pixels (8 bytes) at a time
(0..width).into_par_iter().map(|y|{
let mut tmp_r0 = [f64x2(0.0,0.0);32];
let mut row = vec![0 as u8; (width/8) as usize];
let init_i = f64x2(i0[y], i0[y]);
for x in 0..width/64{
tmp_r0.copy_from_slice(&r0[32*x..32*x+32]);
mand64(&tmp_r0, init_i, &mut row[8*x..8*x + 8]);
}
row
}).collect()
}else {
// process 8 pixels (1 byte) at a time
(0..width).into_par_iter().map(|y|{
let mut tmp_r0 = ZEROS;
let mut row = vec![0 as u8; (width/8) as usize];
let init_i = f64x2(i0[y], i0[y]);
for x in 0..width/8{
tmp_r0.copy_from_slice(&r0[4*x..4*x+4]);
row[x] = mand8(&tmp_r0, init_i);
}
row
}).collect()
};
let stdout_unlocked = std::io::stdout();
let mut stdout = stdout_unlocked.lock();
for row in rows{
stdout.write_all(&row).unwrap();
}
stdout.flush().unwrap();
}
/* The Computer Language Benchmarks Game
http://benchmarksgame.alioth.debian.org/
Contributed by Robert F. Tobler
Modified to use SIMD vectors by Anthony Lloyd and Paul Westcott
*/
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
public static class NBody
{
const double dt = 0.01;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static double[] createBodies()
{
const double Pi = 3.141592653589793;
const double Solarmass = 4 * Pi * Pi;
const double DaysPeryear = 365.24;
const double jmass = 9.54791938424326609e-04 * Solarmass;
const double jvx = 1.66007664274403694e-03 * DaysPeryear;
const double jvy = 7.69901118419740425e-03 * DaysPeryear;
const double jvz = -6.90460016972063023e-05 * DaysPeryear;
const double smass = 2.85885980666130812e-04 * Solarmass;
const double svx = -2.76742510726862411e-03 * DaysPeryear;
const double svy = 4.99852801234917238e-03 * DaysPeryear;
const double svz = 2.30417297573763929e-05 * DaysPeryear;
const double umass = 4.36624404335156298e-05 * Solarmass;
const double uvx = 2.96460137564761618e-03 * DaysPeryear;
const double uvy = 2.37847173959480950e-03 * DaysPeryear;
const double uvz = -2.96589568540237556e-05 * DaysPeryear;
const double nmass = 5.15138902046611451e-05 * Solarmass;
const double nvx = 2.68067772490389322e-03 * DaysPeryear;
const double nvy = 1.62824170038242295e-03 * DaysPeryear;
const double nvz = -9.51592254519715870e-05 * DaysPeryear;
return new double[] {
// sun
(jvx * jmass + svx * smass + uvx * umass + nvx * nmass)/-Solarmass,
(jvy * jmass + svy * smass + uvy * umass + nvy * nmass)/-Solarmass,
(jvz * jmass + svz * smass + uvz * umass + nvz * nmass)/-Solarmass,
0.0,
0.0, 0.0, 0.0, 0.0,
Solarmass,
// jupiter
jvx, jvy, jvz, 0.0,
4.84143144246472090e+00, -1.16032004402742839e+00, -1.03622044471123109e-01, 0.0,
jmass,
// saturn
svx, svy, svz, 0.0,
8.34336671824457987e+00, 4.12479856412430479e+00, -4.03523417114321381e-01, 0.0,
smass,
// uranus
uvx, uvy, uvz, 0.0,
1.28943695621391310e+01, -1.51111514016986312e+01, -2.23307578892655734e-01, 0.0,
umass,
// neptune
nvx, nvy, nvz, 0.0,
1.53796971148509165e+01, -2.59193146099879641e+01, 1.79258772950371181e-01, 0.0,
nmass,
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static double energy(double[] bodies)
{
double e = 0.0;
for(int i=0; i<bodies.Length; i+=9)
{
var Vi = new Vector<double>(bodies,i);
var Xi = new Vector<double>(bodies,i+4);
var imass = bodies[i+8];
e += 0.5 * imass * Vector.Dot(Vi,Vi);
for(int j=i+9; j<bodies.Length; j+=9)
{
var DX = Xi - new Vector<double>(bodies,j+4);
e -= imass * bodies[j+8] / Math.Sqrt(Vector.Dot(DX,DX));
}
}
return e;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void advance(double[] bodies)
{
for(int i=0; i<bodies.Length; i+=9)
{
var Vi = new Vector<double>(bodies,i);
var Xi = new Vector<double>(bodies,i+4);
var MASSi = new Vector<double>(bodies[i+8]);
for(int j=i+9; j<bodies.Length; j+=9)
{
var DX = new Vector<double>(bodies,j+4) - Xi;
var d2 = Vector.Dot(DX, DX);
var MAG = new Vector<double>(dt / (d2 * Math.Sqrt(d2)));
var MASSj = new Vector<double>(bodies[j+8]);
Vi += DX * MASSj * MAG;
(new Vector<double>(bodies,j) - DX * MASSi * MAG).CopyTo(bodies,j);
}
(Vi * dt + Xi).CopyTo(bodies,i+4);
Vi.CopyTo(bodies,i);
}
}
public static void Main(String[] args)
{
var bodies = createBodies();
Console.Out.WriteLineAsync(energy(bodies).ToString("F9"));
int n = args.Length > 0 ? Int32.Parse(args[0]) : 10000;
while(n-->0) advance(bodies);
Console.Out.WriteLineAsync(energy(bodies).ToString("F9"));
}
}
/* The Computer Language Benchmarks Game
http://benchmarksgame.alioth.debian.org/
contributed by Ralph Ganszky
Added dummy element to tuple (because I read an article that this helps
to block less writing back to memory) and store data in
UnsafedMutablePointer to reduce overhead of array access.
*/
import Foundation
typealias Body = (r: (x: Double, y: Double, z: Double),
v: (x: Double, y: Double, z: Double),
m: Double, d: Double)
let nPlanets = 5
let SOLAR_MASS = 4 * Double.pi * Double.pi
let DAYS_PER_YEAR = 365.24
var sun = Body( r: (x: 0.0, y: 0.0, z: 0.0),
v: (x: 0.0, y: 0.0, z: 0.0),
m: SOLAR_MASS, d: 0.0 )
var jupiter = ( r: (x: 4.8414314424647209,
y: -1.16032004402742839,
z: -0.103622044471123109),
v: (x: 1.66007664274403694e-03 * DAYS_PER_YEAR,
y: 7.69901118419740425e-03 * DAYS_PER_YEAR,
z: -6.90460016972063023e-05 * DAYS_PER_YEAR),
m: 9.54791938424326609e-04 * SOLAR_MASS, d: 0.0 )
var saturn = ( r: (x: 8.34336671824457987,
y: 4.12479856412430479,
z: -4.03523417114321381e-01),
v: (x: -2.76742510726862411e-03 * DAYS_PER_YEAR,
y: 4.99852801234917238e-03 * DAYS_PER_YEAR,
z: 2.30417297573763929e-05 * DAYS_PER_YEAR),
m: 2.85885980666130812e-04 * SOLAR_MASS, d: 0.0 )
var uranus = ( r: (x: 1.28943695621391310e+01,
y: -1.51111514016986312e+01,
z: -2.23307578892655734e-01),
v: (x: 2.96460137564761618e-03 * DAYS_PER_YEAR,
y: 2.37847173959480950e-03 * DAYS_PER_YEAR,
z: -2.96589568540237556e-05 * DAYS_PER_YEAR),
m: 4.36624404335156298e-05 * SOLAR_MASS, d: 0.0 )
var neptune = ( r: (x: 1.53796971148509165e+01,
y: -2.59193146099879641e+01,
z: 1.79258772950371181e-01),
v: (x: 2.68067772490389322e-03 * DAYS_PER_YEAR,
y: 1.62824170038242295e-03 * DAYS_PER_YEAR,
z: -9.51592254519715870e-05 * DAYS_PER_YEAR),
m: 5.15138902046611451e-05 * SOLAR_MASS, d: 0.0 )
var bodies = UnsafeMutablePointer<Body>.allocate(capacity: nPlanets)
defer {
bodies.deallocate(capacity: nPlanets)
}
bodies[0] = sun
bodies[1] = jupiter
bodies[2] = saturn
bodies[3] = uranus
bodies[4] = neptune
func advance(_ bodies: UnsafeMutablePointer<Body>, n: Int, dt: Double) {
for i in 0..<n {
let iBody = bodies[i]
for j in i+1..<n {
let jBody = bodies[j]
let (dx, dy, dz) = (iBody.r.x - jBody.r.x,
iBody.r.y - jBody.r.y,
iBody.r.z - jBody.r.z)
let dSquared = dx*dx + dy*dy + dz*dz
let distance = sqrt(dSquared)
let mag = dt / (dSquared * distance)
bodies[i].v = (bodies[i].v.x - dx * jBody.m * mag,
bodies[i].v.y - dy * jBody.m * mag,
bodies[i].v.z - dz * jBody.m * mag)
bodies[j].v = (bodies[j].v.x + dx * iBody.m * mag,
bodies[j].v.y + dy * iBody.m * mag,
bodies[j].v.z + dz * iBody.m * mag)
}
}
for i in 0..<n {
bodies[i].r = (bodies[i].r.x + dt * bodies[i].v.x,
bodies[i].r.y + dt * bodies[i].v.y,
bodies[i].r.z + dt * bodies[i].v.z)
}
}
func energy(_ bodies: UnsafeMutablePointer<Body>, n: Int) -> Double {
var energy = 0.0
for i in 0..<n {
let ibody = bodies[i]
energy += 0.5 * ibody.m * ( ibody.v.x * ibody.v.x +
ibody.v.y * ibody.v.y +
ibody.v.z * ibody.v.z)
for j in i+1..<n {
let jbody = bodies[j]
let dx = ibody.r.x - jbody.r.x
let dy = ibody.r.y - jbody.r.y
let dz = ibody.r.z - jbody.r.z
let distance = sqrt(dx*dx + dy*dy + dz*dz)
energy -= (ibody.m * jbody.m) / distance
}
}
return energy
}
let n: Int
if CommandLine.argc > 1 {
n = Int(CommandLine.arguments[1]) ?? 1000
} else {
n = 1000
}
// Adjust momentum of the sun
var p = (0.0, 0.0, 0.0)
for i in 0..<nPlanets {
p.0 += bodies[i].v.x * bodies[i].m
p.1 += bodies[i].v.y * bodies[i].m
p.2 += bodies[i].v.z * bodies[i].m
}
bodies[0].1 = (-p.0 / SOLAR_MASS, -p.1 / SOLAR_MASS, -p.2 / SOLAR_MASS)
print(String(format: "%.9f", energy(bodies, n: nPlanets)))
for _ in 0..<n {
advance(bodies, n: nPlanets, dt: 0.01)
}
print(String(format: "%.9f", energy(bodies, n: nPlanets)))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment