Creating Multithreaded Skyrim Mods

From the CreationKit Wiki
Revision as of 22:01, 15 January 2015 by imported>Chesko (A tutorial for creating effective multithreaded scripts in Papyrus)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Under Construction - Incomplete

This tutorial is intended to show how modders can use Papyrus more effectively by leveraging its inherent multithreading capability. This guide includes plenty of examples and explanations to help you understand the design pattern. Using multithreading can greatly increase the performance of complex or repetitive calculations, reduce overall strain on the Papyrus VM (making you a better mod-citizen), and help execute time-sensitive tasks.

Papyrus is a threaded scripting language. However, it can be a challenge harness this attribute of the language.

The intended audience for this guide is intermediate to expert Papyrus developers. This design pattern requires SKSE for its use of Mod Events.

The examples provided are intended to be used as a reference to adapt to your own needs; as each mod's needs are different, and because of the way Papyrus (and Skyrim) is designed, writing a generic framework that provides a solution for everyone is not possible; change it to fit your unique requirements. Please take your time going through this guide; there is a lot of information, but once you've grasped the idea, you'll be up and running in no time. There are a lot of codependencies, so some of what you'll be doing may not make sense until the end.


Should I Multithread?

The first question to answer is whether or not a multithreading solution is a good fit for your mod. There's no sense in refactoring hundreds of lines of code if you're not going to stand to benefit from it.

Does your mod:

  • Have many objects that must be placed quickly using things like MoveTo() or PlaceAtMe()?
  • Extensively or repeatedly use latent functions?
  • Have time-critical tasks that rely on the results of other (potentially slow) functions?
  • Have need of doing the same thing to a large group of objects?

If you answered "yes" to any of these bullets, a multithreaded design pattern may increase the performance of your mod. Multithreading excels at taking tasks that would otherwise be run in sequence and making them run simultaneously. I have personally seen performance over 10 times faster (an action that once took ~8.5 seconds to now takes ~0.5 seconds) in Campfire using this method.


Key Terms

  • Thread - An individual script instance that does work. Returns results to a Future.
  • Thread Manager - A script that controls which thread handles a task. Returns a Future to the user of the script.
  • Future - An object that will contain the result of a thread's task, at some point in the future.
  • Future Anchor - An object reference in an unused cell that we use to create Futures with using PlaceAtMe().


A Private Army

Our example problem is that we are developing a Conjuration mod, and we need to spawn 20 guards very quickly when the player casts a spell; ideally, they should all appear at close to the same time. We also need to keep track of the guards we create, so we can destroy them after the spell ends. This guide will not cover creating a spell, instead we will skip to a point after we've created our Spell and our MagicEffect that we want to add a script to.

We come up with the following script to drop onto our MagicEffect in the Creation Kit when our spell is cast:

scriptname SummonArmy extends ActiveMagicEffect

ActorBase property Guard auto
ObjectReference property GuardMarker auto
Actor Guard1
Actor Guard2
...
Actor Guard20

Event OnEffectStart(Actor akTarget, Actor akCaster)
	if akCaster == Game.GetPlayer()
		;Place actors according to the player's position, taking into account walls, obstacles, etc
		MoveGuardMarkerNearPlayer(1) 	;Moves the GuardMarker where the guard is supposed to go; maybe some GetPositions, etc
		Guard1 = GuardMarker.PlaceAtMe(Guard)
		MoveGuardMarkerNearPlayer(2)
		Guard2 = GuardMarker.PlaceAtMe(Guard)
		...
		MoveGuardMarkerNearPlayer(20)
		Guard20 = GuardMarker.PlaceAtMe(Guard)
	endif
endEvent

Event OnEffectFinish(Actor akTarget, Actor akCaster)
	if akCaster == Game.GetPlayer()
		Guard1.Disable()
		Guard1.Delete()
		...
		Guard20.Disable()
		Guard20.Delete()
	endif
endEvent

We test this in-game, and we see each guard appear one-by-one. Kinda lame. You decide to upload it anyway, and your users complain that the spell is "slow" and "clunky".

PlaceAtMe() can be slow, especially over this many objects. We also have (for illustration purposes) some preprocessing that needs to happen (MoveGuardMarkerNearPlayer(int Index)) before we know where to put the guard. We have decided that multithreading this task would be much faster than placing each Actor one-by-one.


Creation Kit

Create Quest: Begin by opening the Creation Kit and creating a new Quest. We'll call our quest GuardPlacementQuest. Click OK to save and close the quest, then open it again (to prevent the CK from crashing). Make sure that "Start Game Enabled", "Run Once", "Warn on Alias Failure" and "Allow repeated stages" are unchecked. Click OK to close it again.

Create Future (Activator): Next, we want to create an object we will need later, called a Future. We'll get into what these do later. Open the Activator tree in the Creation Kit Object Window, and find 'xMarkerActivator'. Right click and Duplicate this object. Double-click the duplicate and rename it's Editor ID to identify it later; we'll call ours GuardPlacementFutureActivator. Click 'No' when asked if you want to create a new form. Click 'Yes' to confirm.

Create Anchor (Object Reference): We now want to create a "Future Anchor". This is an XMarker object reference that we will be placing in a far-off, unused cell. You can create your own blank cell, but AAADeleteWhenDoneTestJeremy is also a good candidate. Wherever you decide to place it, drag an XMarker Static from the Object Window of the Creation Kit and name the reference. We'll name ours GuardPlacementFutureAnchor. We'll use this to PlaceAtMe() Futures on this object later on.


Threads

The thread is what will perform the work we want to perform in parallel. Just like PlaceAtMe(), we expect the result of our Thread to be an ObjectReference.

First, let's define a base Thread "class", called GuardPlacementThread.


scriptname GuardPlacementThread extends Quest

ObjectReference future
int thread_id = -1
bool thread_queued = false

ActorBase theGuard
Static theMarker

ObjectReference function get_async(Activator akFuture, ObjectReference akFutureAnchor, ActorBase akGuard, Static akXMarker)
	thread_queued = true
	if thread_id == -1
		thread_id = GetThreadId()
	endif
	
	theGuard = akGuard
	theMarker = akXMarker

	;Raise the event that will start this thread
	RaiseEvent_OnGuardPlacement(thread_id)

	;Create the Future that will contain our result
	future = akFutureAnchor.PlaceAtMe(akFuture)
	return future
endFunction

bool function busy()
	return thread_queued
endFunction

Event OnGuardPlacement(int aiThreadId)
	if thread_queued && aiThreadId == thread_id
		;OK, let's get some work done!
		ObjectReference tempMarker = Game.GetPlayer().PlaceAtMe(theMarker)		;We could have passed PlayerRef in as a get_async() parameter, too
		MoveGuardMarkerNearPlayer(tempMarker)
		ObjectReference result = tempMarker.PlaceAtMe(theGuard)
		(future as GuardPlacementFuture).result = result
		clear_thread_vars()
		thread_queued = false
	endif
endEvent

function clear_thread_vars()
	;Reset all thread variables to default state
	theGuard = None
	theMarker = None
endFunction

function MoveGuardMarkerNearPlayer(ObjectReference akMarker)
	;Expensive SetPosition, GetPosition, FindNearestRef, etc calls here (illustration only)
endFunction


As you can see, our thread does a few important things:

  • It has a get_async() function, which takes in all of the parameters necessary to do the work we need to perform.
  • It grabs a unique thread_id if it doesn't already have one, which is used to act only on an Event raised by this thread.
  • It defines and registers for an OnGuardPlacement Event, which is a custom Mod Event that does the work.
  • get_async() returns a Future back to the Thread Manager (who will in turn give the Future back to our script).
  • It does some work in the Event, but only if the thread has been 'queued' and the ThreadId of the Event matches ours.
  • We return our results back to the Future we created.
  • We clear all of our member variables using clear_thread_vars().
  • We set thread_queued back to False, which tells the Thread Manager that this thread is available to be used again.

Raising an Event against itself allows the Event OnGuardPlacement() to begin working as soon as get_async() is finished. If we called our function that does our work directly from get_async(), the calling script would block until the work was complete, which would defeat the purpose. We use a Thread ID to filter out events from other threads attached to the same Quest that we don't care about.

Now that we've set up our base Thread script, we will create 10 child scripts that will extend this one. They will each contain only one line, the scriptname definition.


;GuardPlacementThread01.psc
scriptname GuardPlacementThread01 extends GuardPlacementThread

;GuardPlacementThread02.psc
scriptname GuardPlacementThread02 extends GuardPlacementThread

...

;GuardPlacementThread09.psc
scriptname GuardPlacementThread09 extends GuardPlacementThread

;GuardPlacementThread10.psc
scriptname GuardPlacementThread10 extends GuardPlacementThread


Once all of your Thread child scripts and your base Thread script is saved and compiled, attach the 10 child scripts to your Quest. (Do NOT attach the parent Thread script, only the children.)

"But wait," you ask. "We need to place 20 guards, but we only have 10 threads. Won't something break?" The Thread Manager, which we'll talk about next, can handle having more work than there are threads!


Thread Manager

We will next define the Thread Manager script. This script handles delegating our work to an available thread. If a thread is not available, it waits until one is.

Since we may have many thread scripts, and it would be tedious to hook up properties we need to do our task in each and every one, define them here instead and we will pass them in as parameters to our threads. The threads themselves do not have to have Creation Kit-resolved properties, saving you some time.

In the end, the function that we call in our Thread Manager will return a Future, which we can use to get our return value later.

scriptname GuardPlacementThreadManager extends Quest

Quest property GuardPlacementQuest auto
{The name of the thread management quest.}

Activator property GuardPlacementFutureActivator auto
{Our Future object.}

ObjectReference property GuardPlacementFutureAnchor auto
{Our Future Anchor object reference.}

Static property XMarker auto
{Tedious to define properties in the threads and hook up in CK over and over, so define things we need here. MoveGuardMarkerNearPlayer() needs XMarkers.}

;Let's cast our threads to local variables so things are less cluttered in our code
GuardPlacementThread01 thread01 = GuardPlacementQuest as GuardPlacementThread01
GuardPlacementThread02 thread02 = GuardPlacementQuest as GuardPlacementThread02
...
GuardPlacementThread09 thread09 = GuardPlacementQuest as GuardPlacementThread09
GuardPlacementThread09 thread10 = GuardPlacementQuest as GuardPlacementThread10


ObjectReference function PlaceConjuredGuardAsync(ActorBase akGuard)
	ObjectReference future
	while !future
		if !thread01.busy()
			future = thread01.get_async(GuardPlacementFutureActivator, GuardPlacementFutureAnchor, akGuard, XMarker)
		elseif !thread02.busy()
			future = thread02.get_async(GuardPlacementFutureActivator, GuardPlacementFutureAnchor, akGuard, XMarker)
		...
		elseif !thread09.busy()
			future = thread09.get_async(GuardPlacementFutureActivator, GuardPlacementFutureAnchor, akGuard, XMarker)
		elseif !thread10.busy()
			future = thread10.get_async(GuardPlacementFutureActivator, GuardPlacementFutureAnchor, akGuard, XMarker)
		else
			;All threads are busy; wait and try again.
			Utility.wait(0.1)
		endif
	endWhile

	return future
endFunction

Here, we call the get_async() function we defined in our threads earlier. The PlaceConjuredGuardAsync() function handles making sure that our work gets delegated to an available thread. The function then returns a Future once an available thread is found.


Back to the Future

A Future, in parallel processing, is the representation of an asynchronous operation. Like the Google App Engine version that this was inspired by, when the Future is created, it will probably not have any results yet. Your script can store a Future and later call the Future object's get_result() function.

A few notes about Futures:

  • Futures are lightweight Activator ObjectReferences that have an attached script that contain the result of a thread.
  • A Future (as written) can have its results retrieved once, after which, the Future is destroyed. Make sure to save your results to your own variables if you will need them later, since the Future will no longer exist. This keeps the number of ObjectReferences created under control (a contributor to save game size bloat).
  • get_result() is blocking; it waits until results are received from the thread, and then returns.
  • You can check if a Future has received a result without blocking the caller by calling the Future's done() function.
  • The result of a Future is the same as the result of any other function in Papyrus, and can return None, false, etc if an error is encountered. Code the result of a Future like you would the result of anything else and anticipate errors accordingly.

Let's create our Future:

scriptname GuardPlacementFuture extends ObjectReference

ObjectReference r
ObjectReference property result hidden
	function set(ObjectReference akResult)
		done = true
		r = akResult
	endFunction
endProperty

bool done = false
bool function done()
	return done
endFunction

ObjectReference function get_result()
	;Terminate the request after 10 seconds, or as soon as we have a result
	int i = 0
	while !done && i < 100
		i += 1
		utility.wait(0.1)
	endWhile
	RegisterForSingleUpdate(0.1)
	return r
endFunction

Event OnUpdate()
	self.Disable()
	self.Delete()
endEvent

We should only interface with the Future using its member functions, done() and get_result().

Tying it All Together

Now that we've created our Threads, our Thread Manager, and our Future script, we can start to put them to work. In our original ActiveMagicEffect script, we did all of our MoveGuardMarkerNearPlayer() and PlaceAtMe() calls in a row, getting a series of Actor references for our guards in return. We're going to modify that slightly to use our shiny new threaded placement system:

scriptname SummonArmy extends ActiveMagicEffect

Quest property GuardPlacementQuest auto
{We need a reference to our quest with the threads and Thread Manager defined.}
ActorBase property Guard auto
ObjectReference property GuardMarker auto
Actor Guard1
Actor Guard2
...
Actor Guard20

Event OnEffectStart(Actor akTarget, Actor akCaster)
	if akCaster == Game.GetPlayer()
		;Place actors according to the player's position, taking into account walls, obstacles, etc

		;Cast the Quest as our Thread Manager and store it
		GuardPlacementThreadManager threadmgr = GuardPlacementQuest as GuardPlacementThreadManager

		;Call PlaceConjuredGuardAsync for each Guard and store the returned Future
		ObjectReference Guard1Future = threadmgr.PlaceConjuredGuardAsync(Guard)
		ObjectReference Guard2Future = threadmgr.PlaceConjuredGuardAsync(Guard)
		ObjectReference Guard3Future = threadmgr.PlaceConjuredGuardAsync(Guard)
		;...and so on
		ObjectReference Guard19Future = threadmgr.PlaceConjuredGuardAsync(Guard)
		ObjectReference Guard20Future = threadmgr.PlaceConjuredGuardAsync(Guard)

		;Collect the results
		Guard1 = (Guard1Future as GuardPlacementFuture).get_result()
		Guard2 = (Guard2Future as GuardPlacementFuture).get_result()
		Guard3 = (Guard3Future as GuardPlacementFuture).get_result()
		;...and so on
		Guard19 = (Guard19Future as GuardPlacementFuture).get_result()
		Guard20 = (Guard20Future as GuardPlacementFuture).get_result()
	endif
endEvent

Event OnEffectFinish(Actor akTarget, Actor akCaster)
	if akCaster == Game.GetPlayer()
		Guard1.Disable()
		Guard1.Delete()
		;...and so on
		Guard20.Disable()
		Guard20.Delete()
	endif
endEvent

Here, instead of doing the work in our script, we delegated the work to the Thread Manager, and stored the Futures that it returned to us. Then, we gathered the results using our Futures' get_async() function. We don't have to worry about our threads or the created futures; those are freed up and cleared for us by the system.

Even though all of the threads are working in parallel and might not finish at the same time, the get_result() function will wait until a result is available before returning. We can be sure that we will get the results even if they are processed out of order. For instance, if thread 2 completed before thread 1, calling the thread 1 Future's get_result() function will pause the script until a result is available. Then the thread 2 Future's result is gathered, and so on.

Final Notes

  • Be a good Papyrus and Skyrim citizen and read the results from your Futures as soon as you are able so that they can be disposed of. If Futures begin to pile up without being read and destroyed, save game bloat could occur.
  • If you are running operations in an always-on background script that you want to multithread, and you will always have the same number of results back, it may make more sense for you to implement a static set of Future references that are never destroyed that you continue to reuse. This would prevent the churn of Futures being created and destroyed and may lend itself to faster performance. Keep in mind that this would probably result in some data loss if your Futures are not read from regularly as the new results overwrite the old ones.
  • If something doesn't seem to be working correctly, turn on debugging and insert debug.trace() messages where you think your code may be failing. Running the game in windowed mode and using a real-time log viewer like SnakeTail is extremely helpful for diagnosing script problems.
  • You can create as many threads as you want, but I wouldn't recommend more than 50 or so. It depends on your needs, the strain each thread places on the Papyrus VM, and how quickly you need your results.
  • If you need to perform a set of actions that are not all the same, the Thread Manager pattern might not be best for you. You may want to create different thread base scripts purpose-built for several tasks and then call their get_async() functions directly, blocking on busy() until they're available. You can still run many different tasks concurrently this way, even if they're not the same.

Conclusion

I hope this tutorial helps shed light on how you might implement your own multithreaded design pattern in your mods to tackle repetitive, resource-hungry tasks. Feel free to [chesko.tesmod@gmail.com shoot me an email] if you have a question about the examples presented here, if you see an error, or just want to say hi. Good luck, and happy modding!

- Chesko