File: AdunMultithreadedNonbondedTerm.m

package info (click to toggle)
adun.app 0.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 6,824 kB
  • ctags: 713
  • sloc: objc: 49,683; ansic: 4,680; sh: 523; python: 79; makefile: 67; cpp: 33
file content (241 lines) | stat: -rw-r--r-- 4,980 bytes parent folder | download
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "AdunKernel/AdunMultithreadedNonbondedTerm.h"

/**
Contains methods that can be executed on the threaded
object without having to send them to the thread itself
*/
@interface AdNonbondedTerm (MultithreadingExtensions)
/**
Starts a runloop - used to spawn the threaded object
*/
- (void) runThreadedLoop: (id) obj;
/**
Tells the threaded object it will recieve a calculation request
*/
- (void) shouldBeginCalculation;
/**
Asks the threaded object if it has finished a calculation
*/
- (BOOL) hasFinished;
/**
Terminates the thread
*/
- (void) endThreadedLoop;
/**
Tells the threaded object to prepare for another calculation.
*/
- (void) reset;
@end

/**
Each nonbonded term will have to have a special 
implementation of this method
*/
@interface AdNonbondedTerm (ThreadedCopy)
/**
Returns a copy of the nonbonded term that can be used
with AdMultithreadedNonbondedTerm.
*/
- (id) threadCopy;
@end

@implementation AdMultithreadedNonbondedTerm

- (NSArray*) _dividePairs: (id) pairs
{
	int i;
	NSMutableArray* array1, *array2;

	array1 = [NSMutableArray array];
	array2 = [NSMutableArray array];

	for(i=0; i<(int)[pairs count];i++)
	{
		if(i%2 == 0)
		{
			[array1 addObject: [pairs objectAtIndex: i]];
			[array2 addObject: [NSIndexSet indexSet]];
		}	
		else	
		{
			[array1 addObject: [NSIndexSet indexSet]];
			[array2 addObject: [pairs objectAtIndex: i]];
		}	
	}

	return [NSArray arrayWithObjects: array1, array2, nil];
}

- (id) initWithDictionary: (NSDictionary*) dict
{
	return [self initWithTerm: [dict objectForKey: @"term"]];
}

- (id) initWithTerm: (AdNonbondedTerm*) nonbondedTerm
{
	if((self = [super init]))
	{
		mainThread = [nonbondedTerm retain];
		threadedObj = [nonbondedTerm threadCopy];
		dividedPairs = [self _dividePairs: [mainThread nonbondedPairs]];
		[dividedPairs retain];
		[mainThread setNonbondedPairs: [dividedPairs objectAtIndex: 0]];
		[threadedObj setNonbondedPairs: [dividedPairs objectAtIndex: 1]];
		[NSThread detachNewThreadSelector: @selector(runThreadedLoop:)
			toTarget: threadedObj
			withObject: nil];
	}

	return self;
}

- (void) dealloc
{
	[threadedObj endThreadedLoop];
	[dividedPairs release];
	[mainThread release];
	[threadedObj release];
	[super dealloc];
}

- (void) evaluateEnergy
{
	//The threaded obj will only calculated forces
	/*[threadedObj shouldBeginCalculation];
	[mainThread evaluateEnergy];
	while(![threadedObj hasFinished])
		continue;
	
	[threadedObj reset];*/

	[mainThread evaluateEnergy];
}

- (void) evaluateForces
{
	int i,j;
	AdMatrix* forces1, *forces2;

	[threadedObj clearForces];
	[mainThread clearForces];

	[threadedObj shouldBeginCalculation];
	[mainThread evaluateForces];
	while(![threadedObj hasFinished])
		continue;

	[threadedObj reset];

	forces1 = [mainThread forces];
	forces2 = [threadedObj forces];
	for(i=0; i<forces->no_rows; i++)
		for(j=0; j<3; j++)
			forces->matrix[i][j] += (forces2->matrix[i][j] + forces1->matrix[i][j]);
}

- (void) setExternalForceMatrix: (AdMatrix*) matrix
{
	forces = matrix;
}

- (double) electrostaticEnergy;
{
	return [mainThread electrostaticEnergy] + [threadedObj electrostaticEnergy];
}	

- (double) lennardJonesEnergy;
{
	return [mainThread lennardJonesEnergy] + [threadedObj lennardJonesEnergy];
}

- (id) system
{
	return [mainThread system];
}

- (NSString*) lennardJonesType
{
	return [mainThread lennardJonesType];
}

@end

@implementation AdNonbondedTerm (MultithreadingExtensions)

/**
This is the method that run in the subthread. We communicate with
the subthread thRunLooprough the objects shared instance variables.
The subthread monitors the values of beginThreadedCalculation and
threadFinished and endThread which are set from the main thread.
*/
- (void) runThreadedLoop: (id) obj
{
	NSAutoreleasePool *pool = [NSAutoreleasePool new];
	
	endThread = NO;
	beginThreadedCalculation = NO;
	threadFinished = NO;
	while(!endThread)
	{
		//Polling is bad but this is just a test.
		if(beginThreadedCalculation)
		{
			threadFinished = NO;
			NSDebugLLog(@"Multithread", @"Thread evaluating forces");
			[self evaluateForces];
			NSDebugLLog(@"Multithread", @"Complete");
			threadFinished = YES;
			beginThreadedCalculation = NO;
		}
	}

	[pool release];
	[NSThread exit];
}
/**
Tells the threaded object it will recieve a calculation request.
Executed on the main thread
*/
- (void) shouldBeginCalculation
{
	beginThreadedCalculation = YES;
}
/**
Asks the threaded object if it has finished
Executed on the main thread
*/
- (BOOL) hasFinished
{
	return threadFinished; 
}

- (void) reset
{
	threadFinished = NO;
}
/**
Terminates the thread
Executed on the main thread
*/
- (void) endThreadedLoop
{
	endThread = YES;
}

@end

@implementation AdPureNonbondedTerm (ThreadedCopy)

- (id) threadCopy
{
	return [[[self class] alloc]
		initWithSystem: system
		cutoff: cutoff
		updateInterval: updateInterval
		permittivity: permittivity
		nonbondedPairs: nil
		externalForceMatrix: NULL
		listHandlerClass: listHandlerClass];
}

@end