Blame view

src/mior/model/OCLCSRModel2.java 4.75 KB
1b1e928cc   glaville   initial import of...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  package mior.model;
  
  import mcmas.core.MCM;
  import mcmas.core.MCMCommandQueue;
  import mcmas.core.MCMCommandQueueProperty;
  import mcmas.core.MCMContext;
  import mcmas.core.MCMEvent;
  import mcmas.core.MCMKernel;
  import mcmas.core.MCMMem;
  import mcmas.core.MCMProgram;
  import mcmas.core.MCMUtils;
  
  import org.jocl.Pointer;
  import org.perf4j.slf4j.Slf4JStopWatch;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  
  public class OCLCSRModel2 extends SimpleMiorModel {
  	
  	final MCMContext context;
  	final MCMCommandQueue queue;
  	final MCMProgram program;
  	
  	final MCMKernel topoKernel;
  	final MCMKernel autoliveKernel;
  	
  	final MCMMem mmMem;
  	final MCMMem omMem;
  	final MCMMem worldMem;
  	
  	final MCMMem mmCSRMem;
  	final MCMMem omCSRMem;
  	final MCMMem associationsMem;
  	
  	final MCMMem partsMem;
  	
  	private final int[] mmCSR;
  	private final int[] omCSR;
  	
  	private final static Logger logger = LoggerFactory.getLogger(OCLCSRModel2.class);
  	private final Slf4JStopWatch watch = new Slf4JStopWatch(logger);
  	
  	private final static String MODEL_SOURCE = "kernels/mior_model_csr.cl";
  	
  	public OCLCSRModel2(MiorWorld world) {
  		super(world);
  		
  		watch.start("OpenCL setup");
  		
  		this.context = new MCMContext();
  		this.queue = context.createCommandQueue(MCMCommandQueueProperty.ENABLE_PROFILING);
  		
  		watch.stop();
  		
  		watch.start("Program compilation");
  		this.program = MCMUtils.compileFile(context, MODEL_SOURCE, " -DNBMM=" + nbMM + " -DNBOM=" + nbOM);
  		watch.stop();
  		
  		watch.start("Kernel allocation");
  		
  		this.topoKernel = program.createKernel("topology");
  		this.autoliveKernel = program.createKernel("autolive");
  		
  		watch.stop();
  		watch.start("Memory allocation");
  		
  		this.mmMem = context.newBuffer().Using(mmList).b();
  		this.omMem = context.newBuffer().Using(omList).b();
  		this.worldMem = context.newBuffer().Using(world).b();
  		
  		this.mmCSR = MiorUtils.allocateCSRStorage(nbOM, nbMM);
  		this.mmCSRMem = context.newBuffer().Using(mmCSR).b();
  		
  		this.omCSR = MiorUtils.allocateCSRStorage(nbMM, nbOM);
  		this.omCSRMem = context.newBuffer().Using(omCSR).b();
  		
  		this.associationsMem = context.newBuffer().Using(associations).b();
  		
  		this.partsMem = context.newBuffer().Size(nbMM * nbOM, MCM.INT).b();
  		watch.stop();
  	}
  	
  	@Override
  	protected void resetImpl() {
  		super.resetImpl();
  		
  		MiorUtils.initCSRStorage(mmCSR);
  		MiorUtils.initCSRStorage(omCSR);
  		
  		queue.enqueueWriteBuffer(mmMem, mmList, 0, mmMem.getSize());
  		queue.enqueueWriteBuffer(omMem, omList, 0, omMem.getSize());
  		queue.enqueueWriteBuffer(mmCSRMem, Pointer.to(mmCSR), 0, mmCSRMem.getSize());
  		queue.enqueueWriteBuffer(omCSRMem, Pointer.to(omCSR), 0, omCSRMem.getSize());
  		queue.enqueueWriteBuffer(worldMem, new MiorWorld[] { world }, 0, worldMem.getSize());
  		queue.enqueueWriteBuffer(associationsMem, Pointer.to(associations), 0, associationsMem.getSize());
  		queue.finish();
  	}
  	
  	@Override
  	protected void doTopologyImpl() {
  		topoKernel.setArguments(mmMem, omMem, worldMem, mmCSRMem, omCSRMem, associationsMem);
  		
  		/*
  		OCLEvent event = queue.enqueueKernel(
  				topoKernel, 2,
  				new long[] { 0, 0 },
  				new long[] { mmList.length, omList.length },
  				new long[] { 1, 1 }
  		);*/
  		
  		MCMEvent event = queue.enqueueKernel(topoKernel, 2, new long[] { nbMM, nbOM });
  		
  		MCMEvent.waitFor(event);
  		MCMUtils.printEventStats("topology", event);
  		
  		if (! isBatchModeEnabled()) {
  			queue.blockingReadBuffer(mmCSRMem, Pointer.to(mmCSR), 0, mmCSRMem.getSize());
  			queue.blockingReadBuffer(omCSRMem, Pointer.to(omCSR), 0, omCSRMem.getSize());
  			queue.blockingReadBuffer(associationsMem, Pointer.to(associations), 0, associationsMem.getSize());
  		}
  	}
  	
  	@Override
  	protected void doLiveImpl() {
  		throw new UnsupportedOperationException();
  	}
  
  	@Override
  	public void releaseImpl() {
  		partsMem.release();
  		
  		associationsMem.release();
  		omCSRMem.release();
  		mmCSRMem.release();
  		
  		worldMem.release();
  		omMem.release();
  		mmMem.release();
  		
  		autoliveKernel.release();
  		topoKernel.release();
  		
  		program.release();
  		queue.release();
  		context.release();
  	}
  	
  	@Override
  	public void doAutoLive() {
  		reset();
  		doTopology();
  		
  		autoliveKernel.setArguments(mmMem, omMem, worldMem, mmCSRMem, omCSRMem, partsMem);
  		MCMEvent event = queue.enqueue1DKernel(autoliveKernel, Math.max(nbOM, nbMM));
  		
  		MCMEvent.waitFor(event);
  		MCMUtils.printEventStats("autolive", event);
  		
  		onSimulationFinished();
  	}
  	
  	@Override
  	protected void onSimulationFinished() {
  		if (isBatchModeEnabled()) {
  			queue.blockingReadBuffer(mmMem, mmList, 0, mmMem.getSize());
  			queue.blockingReadBuffer(omMem, omList, 0, omMem.getSize());
  			queue.blockingReadBuffer(worldMem, new MiorWorld[] {world}, 0, worldMem.getSize());
  			queue.blockingReadBuffer(associationsMem, Pointer.to(associations), 0, associationsMem.getSize());
  		}
  		
  		super.onSimulationFinished();
  	}
  
  }