Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

74
LINES

< > BotCompany Repo | #1013705 // Multi-threaded Mandelbrot (outputs a PBM file on console)

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (997L/7K).

1  
!7
2  
3  
/* The Computer Language Benchmarks Game
4  
 * http://benchmarksgame.alioth.debian.org/
5  
 *
6  
 * contributed by Stefan Krause
7  
 * slightly modified by Chad Whipkey
8  
 * parallelized by Colin D Bennett 2008-10-04
9  
 * reduce synchronization cost by The Anh Tran
10  
 * optimizations and refactoring by Enotus 2010-11-11
11  
 * optimization by John Stalcup 2012-2-19
12  
 */
13  
14  
static byte[][] out;
15  
static AtomicInteger yCt;
16  
static double[] Crb;
17  
static double[] Cib;
18  
19  
static int getByte(int x, int y){
20  
  int res=0;
21  
  for(int i=0;i<8;i+=2){
22  
     double Zr1=Crb[x+i];
23  
     double Zi1=Cib[y];
24  
25  
     double Zr2=Crb[x+i+1];
26  
     double Zi2=Cib[y];
27  
28  
     int b=0;
29  
     int j=49;do{
30  
        double nZr1=Zr1*Zr1-Zi1*Zi1+Crb[x+i];
31  
        double nZi1=Zr1*Zi1+Zr1*Zi1+Cib[y];
32  
        Zr1=nZr1;Zi1=nZi1;
33  
34  
        double nZr2=Zr2*Zr2-Zi2*Zi2+Crb[x+i+1];
35  
        double nZi2=Zr2*Zi2+Zr2*Zi2+Cib[y];
36  
        Zr2=nZr2;Zi2=nZi2;
37  
38  
        if(Zr1*Zr1+Zi1*Zi1>4){b|=2;if(b==3)break;}
39  
        if(Zr2*Zr2+Zi2*Zi2>4){b|=1;if(b==3)break;}
40  
     }while(--j>0);
41  
     res=(res<<2)+b;
42  
  }
43  
  return res^-1;
44  
}
45  
46  
static void putLine(int y, byte[] line){
47  
  for (int xb=0; xb<line.length; xb++)
48  
     line[xb]=(byte)getByte(xb*8,y);
49  
}
50  
51  
p {
52  
  int N=6000;
53  
  if (args.length>=1) N=Integer.parseInt(args[0]);
54  
55  
  Crb=new double[N+7]; Cib=new double[N+7];
56  
  double invN=2.0/N; for(int i=0;i<N;i++){ Cib[i]=i*invN-1.0; Crb[i]=i*invN-1.5; }
57  
  yCt=new AtomicInteger();
58  
  out=new byte[N][(N+7)/8];
59  
60  
  Thread[] pool=new Thread[2*Runtime.getRuntime().availableProcessors()];
61  
  for (int i=0;i<pool.length;i++)
62  
     pool[i]=new Thread(){
63  
        public void run() {
64  
            int y; while((y=yCt.getAndIncrement())<out.length) putLine(y,out[y]);
65  
        }
66  
     };
67  
  for (Thread t:pool) t.start();
68  
  for (Thread t:pool) t.join();
69  
70  
  OutputStream stream = new BufferedOutputStream(System.out);
71  
  stream.write(("P4\n"+N+" "+N+"\n").getBytes());
72  
  for(int i=0;i<N;i++) stream.write(out[i]);
73  
  stream.close();
74  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1013705
Snippet name: Multi-threaded Mandelbrot (outputs a PBM file on console)
Eternal ID of this version: #1013705/2
Text MD5: 9d637df567d95b98f828f836b693a961
Transpilation MD5: 482b3f06ca0646d9eea736a03c5c6797
Author: stefan
Category: javax / imaging
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-01-25 19:55:49
Source code size: 2011 bytes / 74 lines
Pitched / IR pitched: No / No
Views / Downloads: 516 / 1189
Version history: 1 change(s)
Referenced in: [show references]