Making Xgrid synchronous
I finally got around to making the DRMAA code actually, um, talk to Xgrid. Right now all it does is open and close connections, but everything else will follow pretty much the same pattern.
From the client code, it’s very easy to begin a DRMAA session:
DRMAASession *session = [[DRMAASystem system] session];
NSError *error = nil;
BOOL success = [session begin:&error];
That translates into a whole pile of XgridFoundation code which, in summary, does the following:
-
First, the code spawns a second thread, passing it a couple of ports through which to send Distributed Objects messages, and setting up a DO proxy object on the main thread. The second thread runs a standard
NSRunLooploop, which does two things: (1) gives the XgridFoundation objects the opportunity to do their thing, and (2) receives DO messages from the first thread, removing the need for dealing with mutex locks. -
The Xgrid thread having started its run loop, the main thread sends it a message to establish a connection with the Xgrid controller. In the Xgrid thread, a connection is opened with the usual XgridFoundation calls (using settings stored in user defaults as described in the previous post). Instead of using the delegate method callbacks, which would mean I’d need to signal the first thread with a condition variable, I wrote the code to simply run the run loop until the Xgrid connection was in the “open” state (or the “closed” state on account of an error). I was surprised this worked: even though the method is being called as part of
-[NSRunLoop run...](since it’s a DO message), it can itself run the run loop. - Once the connection has (hopefully) opened successfully, the second thread’s DO-called method returns, letting the main thread continue. No callbacks, no nothing—one call, and the main thread knows whether the connection succeeded or not.
(The session-closing code just calls a method via DO in similar fashion.)
XgridDRMAA should make it really easy for procedural Objective-C (no, not necessarily an oxymoron) programs to use Xgrid. (C and Java too, of course, once the wrapping is done.) GridEZ is much better for real interactive Cocoa GUI apps—you don’t want to block your main thread waiting for a response from Xgrid, obviously—but for many scientists, this model is useful.