<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://info.stat.cmu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ecucuzze</id>
	<title>Stat DS Computing Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://info.stat.cmu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ecucuzze"/>
	<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Special:Contributions/Ecucuzze"/>
	<updated>2026-11-05T03:25:16Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.9</generator>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=What_resources_when&amp;diff=273</id>
		<title>What resources when</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=What_resources_when&amp;diff=273"/>
		<updated>2025-07-29T17:02:59Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
To try to answer the common question “what resources do I have access to and when should I use them”, below we present to approaches to answering the question. The first of which focuses on what resources exist and what they should be used for and the second tries to answer resource needs / current computing problems with resources to fit those needs/ solve those problems.&lt;br /&gt;
&lt;br /&gt;
After you’re found the resource you think you’d like to use (or even when you’re still deciding), we strongly encourage you to attend an computing office hour to help discuss all the things you should be thinking about and get you on the right track quickly.&lt;br /&gt;
&lt;br /&gt;
= What computing resources do you have access to =&lt;br /&gt;
&lt;br /&gt;
== Your personal computer ==&lt;br /&gt;
&lt;br /&gt;
Your personal computer is great for small jobs that take less than 30 minutes, uses few cores and not too much RAM.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pro:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#Naturally the easiest tool to use since it’s yours. Best for short jobs that require only a few cores. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Cons:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#&lt;br /&gt;
capacity:&lt;br /&gt;
&lt;br /&gt;
#*few cores: 4-8 cores common on laptops&amp;lt;ref&amp;gt;&amp;lt;p&amp;gt;From [http://laptopmag.com/articles/Cpu-comparison laptopmag]&amp;lt;/p&amp;gt;&amp;lt;/ref&amp;gt; (multiply processes per cors means = 8-16 “cores”). &lt;br /&gt;
#*low RAM: 4-32 GB of common on laptops&amp;lt;ref&amp;gt;&amp;lt;p&amp;gt;From [http://digitaltrends.com/computing/how-much-ram digitaltrends]&amp;lt;/p&amp;gt;&amp;lt;/ref&amp;gt;, used across all cores &lt;br /&gt;
#*“no” GPUs: you don’t have GPUs that are optimized for Deep Learning &lt;br /&gt;
&lt;br /&gt;
#&lt;br /&gt;
other usage: You often want to use your computer for other things (and it’s built for that exact thing). It’s possible you or someone you know has has had to leave their laptop at home for the day/ run lode oernight When they need to run something intensive -OR- long. But you need that laptop for other work / homework…&lt;br /&gt;
&lt;br /&gt;
The below XKCD comic doesn’t have to be you… (though breaks are good):&lt;br /&gt;
&lt;br /&gt;
[[File:Compiling.png|thumb|none|https://xkcd.com/303/]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Conclusion:&#039;&#039;&#039; Your personal computer isn’t the best for when doing a lot of computing and its easy to put jobs elsewhere!&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Common use-cases&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#Most class projects &lt;br /&gt;
#Non-data problems &lt;br /&gt;
#Non-complex structure problems &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Associated Articles&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*[[Parallel_computing|parallelization]] &lt;br /&gt;
&lt;br /&gt;
== The &amp;lt;code&amp;gt;hydras&amp;lt;/code&amp;gt; (the department servers) ==&lt;br /&gt;
&lt;br /&gt;
The departmental servers should be seen as a “sandbox”. You can experiment with lots of things on the servers and it doesn’t cost you much to do. The hydras have a decent amount of RAM (much more than your personal computer), but often it won’t run a lot faster than if you put you’re computer aside and did the same thing on it (though a large pro is you don’t have to stop using your own computer when running jobs on the hydras).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pros:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#Best for long but smaller core jobs &lt;br /&gt;
#Decent amount of RAM per machine (64 GB, or 128 GB on hydra7 and hydra8)&lt;br /&gt;
#Very low barrier to entry (aka easy to get started) &lt;br /&gt;
#File management system shares across different hydra servers &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Cons:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#usage: The department server is a bit of a “wild-west” in terms of usage (it varies how many people use it at a given time) &lt;br /&gt;
#cores per server: the number of cores per server is only 12&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Conclusion:&#039;&#039;&#039; Great place to start for more intensive computing.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Associated Articles&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*[[Linux_and_department_servers|Linux and department servers]] &lt;br /&gt;
&#039;&#039;&#039;Server Monitoring&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
View current Hydra resource usage - [https://info.stat.cmu.edu/grafana/d/fdkb6l4klj400d/hydra-cluster?orgId=1 Hydra Cluster Usage Monitor]&lt;br /&gt;
&lt;br /&gt;
== PSC (Pittsburgh supercomputer center) ==&lt;br /&gt;
&lt;br /&gt;
The Pittsburgh supercomputer center provides supercomputers than have massive amounts of cores (but also can find some with large RAMs and GPUs). Generally speaking, the supercomputing resources are often used when you have extremely parallelizable code that would take a very long time if you ran it on just a few cores (e.g.&amp;amp;nbsp;your computer or a few hydras).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pros:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#A massive amount of cores and RAM that can be leveraged for extremely parallelizable computing &lt;br /&gt;
#Easier to access in the statistics department &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Cons:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#Requires learning of new job management system (though quick to learn - see [Slurm]) &lt;br /&gt;
#Some options to access the resources can take time or require going through your advisor &lt;br /&gt;
#&#039;&#039;currently (2021)&#039;&#039; has been having problems with servers crashing &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Associated Articles&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*[[PSC|PSC]] &lt;br /&gt;
*[[Slurm|Slurm]] &lt;br /&gt;
&lt;br /&gt;
== GPU Computing ==&lt;br /&gt;
The Statistics &amp;amp; Data Science Department offers two main GPU computing environments.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Ghidorahs&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* 3 physical servers&lt;br /&gt;
* RTX 4090 GPUs&lt;br /&gt;
* Connected to the Stats network share&lt;br /&gt;
* Your statistics directory account will grant you access&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Wright&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* For a detail overview, please check [[GPU Cluster|here]]&lt;br /&gt;
* You must request access&lt;br /&gt;
* The Statistics network share is not accessible &lt;br /&gt;
&lt;br /&gt;
= Computing resource flowchart =&lt;br /&gt;
&lt;br /&gt;
These flow charts try to help capture what’s available and when you might use them.&lt;br /&gt;
&lt;br /&gt;
Visit the links below to view current computing resource usage. Select a server with the least amount of users and lowest resource usage.&lt;br /&gt;
&lt;br /&gt;
* [https://info.stat.cmu.edu/grafana/d/fdkb6l4klj400d/hydra-cluster?orgId=1 Hydra Cluster Usage Monitor]&lt;br /&gt;
&lt;br /&gt;
* [https://info.stat.cmu.edu/grafana/d/edkar1rgy3l6od/ghidorah-cluster?orgId=1 Ghidorah Cluster Usage Monitor]&lt;br /&gt;
[[File:ProblemsOnYourPersonalComputerUpdated.png|center|thumb|1000x1000px]]&lt;br /&gt;
&lt;br /&gt;
From [http://laptopmag.com/articles/Cpu-comparison laptopmag]&lt;br /&gt;
&amp;lt;br/&amp;gt; &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2. &amp;lt;font color=&amp;quot;blue&amp;quot;&amp;gt;↑&amp;lt;/font&amp;gt; &lt;br /&gt;
From [http://digitaltrends.com/computing/how-much-ram digitaltrends]&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=File:ProblemsOnYourPersonalComputerUpdated.png&amp;diff=272</id>
		<title>File:ProblemsOnYourPersonalComputerUpdated.png</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=File:ProblemsOnYourPersonalComputerUpdated.png&amp;diff=272"/>
		<updated>2025-07-29T17:01:45Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Problems on Your Personal Computer&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=What_resources_when&amp;diff=271</id>
		<title>What resources when</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=What_resources_when&amp;diff=271"/>
		<updated>2025-07-29T16:25:53Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: /* Azure */ removed Azure&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
To try to answer the common question “what resources do I have access to and when should I use them”, below we present to approaches to answering the question. The first of which focuses on what resources exist and what they should be used for and the second tries to answer resource needs / current computing problems with resources to fit those needs/ solve those problems.&lt;br /&gt;
&lt;br /&gt;
After you’re found the resource you think you’d like to use (or even when you’re still deciding), we strongly encourage you to attend an computing office hour to help discuss all the things you should be thinking about and get you on the right track quickly.&lt;br /&gt;
&lt;br /&gt;
= What computing resources do you have access to =&lt;br /&gt;
&lt;br /&gt;
== Your personal computer ==&lt;br /&gt;
&lt;br /&gt;
Your personal computer is great for small jobs that take less than 30 minutes, uses few cores and not too much RAM.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pro:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#Naturally the easiest tool to use since it’s yours. Best for short jobs that require only a few cores. &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Cons:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#&lt;br /&gt;
capacity:&lt;br /&gt;
&lt;br /&gt;
#*few cores: 4-8 cores common on laptops&amp;lt;ref&amp;gt;&amp;lt;p&amp;gt;From [http://laptopmag.com/articles/Cpu-comparison laptopmag]&amp;lt;/p&amp;gt;&amp;lt;/ref&amp;gt; (multiply processes per cors means = 8-16 “cores”). &lt;br /&gt;
#*low RAM: 4-32 GB of common on laptops&amp;lt;ref&amp;gt;&amp;lt;p&amp;gt;From [http://digitaltrends.com/computing/how-much-ram digitaltrends]&amp;lt;/p&amp;gt;&amp;lt;/ref&amp;gt;, used across all cores &lt;br /&gt;
#*“no” GPUs: you don’t have GPUs that are optimized for Deep Learning &lt;br /&gt;
&lt;br /&gt;
#&lt;br /&gt;
other usage: You often want to use your computer for other things (and it’s built for that exact thing). It’s possible you or someone you know has has had to leave their laptop at home for the day/ run lode oernight When they need to run something intensive -OR- long. But you need that laptop for other work / homework…&lt;br /&gt;
&lt;br /&gt;
The below XKCD comic doesn’t have to be you… (though breaks are good):&lt;br /&gt;
&lt;br /&gt;
[[File:Compiling.png|thumb|none|https://xkcd.com/303/]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Conclusion:&#039;&#039;&#039; Your personal computer isn’t the best for when doing a lot of computing and its easy to put jobs elsewhere!&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Common use-cases&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#Most class projects &lt;br /&gt;
#Non-data problems &lt;br /&gt;
#Non-complex structure problems &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Associated Articles&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*[[Parallel_computing|parallelization]] &lt;br /&gt;
&lt;br /&gt;
== The &amp;lt;code&amp;gt;hydras&amp;lt;/code&amp;gt; (the department servers) ==&lt;br /&gt;
&lt;br /&gt;
The departmental servers should be seen as a “sandbox”. You can experiment with lots of things on the servers and it doesn’t cost you much to do. The hydras have a decent amount of RAM (much more than your personal computer), but often it won’t run a lot faster than if you put you’re computer aside and did the same thing on it (though a large pro is you don’t have to stop using your own computer when running jobs on the hydras).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pros:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#Best for long but smaller core jobs &lt;br /&gt;
#Decent amount of RAM per machine (64 GB, or 128 GB on hydra7 and hydra8)&lt;br /&gt;
#Very low barrier to entry (aka easy to get started) &lt;br /&gt;
#File management system shares across different hydra servers &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Cons:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#usage: The department server is a bit of a “wild-west” in terms of usage (it varies how many people use it at a given time) &lt;br /&gt;
#cores per server: the number of cores per server is only 12&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Conclusion:&#039;&#039;&#039; Great place to start for more intensive computing.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Associated Articles&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*[[Linux_and_department_servers|Linux and department servers]] &lt;br /&gt;
&#039;&#039;&#039;Server Monitoring&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
View current Hydra resource usage - [https://info.stat.cmu.edu/grafana/d/fdkb6l4klj400d/hydra-cluster?orgId=1 Hydra Cluster Usage Monitor]&lt;br /&gt;
&lt;br /&gt;
== PSC (Pittsburgh supercomputer center) ==&lt;br /&gt;
&lt;br /&gt;
The Pittsburgh supercomputer center provides supercomputers than have massive amounts of cores (but also can find some with large RAMs and GPUs). Generally speaking, the supercomputing resources are often used when you have extremely parallelizable code that would take a very long time if you ran it on just a few cores (e.g.&amp;amp;nbsp;your computer or a few hydras).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pros:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#A massive amount of cores and RAM that can be leveraged for extremely parallelizable computing &lt;br /&gt;
#Easier to access in the statistics department &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Cons:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
#Requires learning of new job management system (though quick to learn - see [Slurm]) &lt;br /&gt;
#Some options to access the resources can take time or require going through your advisor &lt;br /&gt;
#&#039;&#039;currently (2021)&#039;&#039; has been having problems with servers crashing &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Associated Articles&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
*[[PSC|PSC]] &lt;br /&gt;
*[[Slurm|Slurm]] &lt;br /&gt;
&lt;br /&gt;
== GPU Computing ==&lt;br /&gt;
The Statistics &amp;amp; Data Science Department offers two main GPU computing environments.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Ghidorahs&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* 3 physical servers&lt;br /&gt;
* RTX 4090 GPUs&lt;br /&gt;
* Connected to the Stats network share&lt;br /&gt;
* Your statistics directory account will grant you access&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Wright&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* For a detail overview, please check [[GPU Cluster|here]]&lt;br /&gt;
* You must request access&lt;br /&gt;
* The Statistics network share is not accessible &lt;br /&gt;
&lt;br /&gt;
= Computing resource flowchart =&lt;br /&gt;
&lt;br /&gt;
These flow charts try to help capture what’s available and when you might use them.&lt;br /&gt;
&lt;br /&gt;
Visit the links below to view current computing resource usage. Select a server with the least amount of users and lowest resource usage.&lt;br /&gt;
&lt;br /&gt;
* [https://info.stat.cmu.edu/grafana/d/fdkb6l4klj400d/hydra-cluster?orgId=1 Hydra Cluster Usage Monitor]&lt;br /&gt;
&lt;br /&gt;
* [https://info.stat.cmu.edu/grafana/d/edkar1rgy3l6od/ghidorah-cluster?orgId=1 Ghidorah Cluster Usage Monitor]&lt;br /&gt;
==  [[File:ProblemsOnYourPersonalComputer11.png|706x706px]]==&lt;br /&gt;
&lt;br /&gt;
From [http://laptopmag.com/articles/Cpu-comparison laptopmag]&lt;br /&gt;
&amp;lt;br/&amp;gt; &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;2. &amp;lt;font color=&amp;quot;blue&amp;quot;&amp;gt;↑&amp;lt;/font&amp;gt; &lt;br /&gt;
From [http://digitaltrends.com/computing/how-much-ram digitaltrends]&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Azure_getting_started&amp;diff=270</id>
		<title>Azure getting started</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Azure_getting_started&amp;diff=270"/>
		<updated>2025-07-29T16:24:58Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: Removed any reference to the department&amp;#039;s azure resources. Made it generic&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
= Azure =&lt;br /&gt;
&lt;br /&gt;
== Respectiful use of Azure resources ==&lt;br /&gt;
&lt;br /&gt;
Ensure you’re not wasting precious department Azure credits - when you are not using your virtual machine, please shut it down (and then relaunch it when you need to use it). You can shut-down and relauch a server on the resource page. The overview for your virtual machine might look something like the image below. To turn off your machine click the “Stop” button on the “Overview” tab, and to launch your machine click the “Start” button. Every time you launch your machine it’s likely that the IP address will have changed, so use the “Connect” button to figure out the new IP address (though it is also listed on the Overview page). The “Connect” page (which appears in the second image) will also remind you of what your username for the virtual machine is.&lt;br /&gt;
&lt;br /&gt;
[[File:1600px-Azure on off.png|frameless|800px|turning on and off server to save department money]]&lt;br /&gt;
&lt;br /&gt;
[[File:1600px-Azure connect.png|frameless|800px|connecting to your server&#039;s IP address when it changes]]&lt;br /&gt;
&lt;br /&gt;
= Getting started: create an Azure virtual machine =&lt;br /&gt;
&lt;br /&gt;
== Creating a Resource ==&lt;br /&gt;
&lt;br /&gt;
You will need to have an established Azure account, which no longer can be done through department resources as of Summer 2025. Creating a virtual on Azure can be a bit complicated (which is why other resources like the department servers are available for certain types of computing).&lt;br /&gt;
&lt;br /&gt;
The first thing to do is click the “create a resource” button on your azure home page (should look something like this:)&lt;br /&gt;
&lt;br /&gt;
[[File:Azure create new.png|frameless|300px|create new server button]]&lt;br /&gt;
&lt;br /&gt;
You can also do so in the &amp;lt;code&amp;gt;All resources&amp;lt;/code&amp;gt; &amp;gt; &amp;lt;code&amp;gt;+ Create&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
For statistical computing we select a resource from the &amp;lt;code&amp;gt;AI + Machine Learning&amp;lt;/code&amp;gt; tab on the left side and then select &amp;lt;code&amp;gt;Data Science Virtual Machine - Ubuntu 18.04&amp;lt;/code&amp;gt; for a linux virtual machine. This selection will look something like the image below:&lt;br /&gt;
&lt;br /&gt;
[[File:Azure select resource type.png|frameless|700px|Selecting which type of virtual server to use]]&lt;br /&gt;
&lt;br /&gt;
=== Defining your Resource ===&lt;br /&gt;
&lt;br /&gt;
There are a lot of options to create your resource, but you can ignore a lot of these.&lt;br /&gt;
&lt;br /&gt;
==== Basics ====&lt;br /&gt;
&lt;br /&gt;
Below is 2 images of the first “Basics” page. You will need to&lt;br /&gt;
&lt;br /&gt;
#Fill out / select your “Resource group”&lt;br /&gt;
#Define your “Virtual machine name” (might we recommend (username)-(CPU/GPU)-cores(#)-(RAM)-(index). For example “bpleroy-GPU-cores4-27-1”.&lt;br /&gt;
#Select the size of the machine (relative to it’s cores and types of cores). You will see available options. You can also click &amp;quot;see all images&amp;quot; to explore a list of options. If you are looking for GPU in this list filter by &amp;quot;family&amp;quot; and &amp;quot;GPU&amp;quot; for the family.&lt;br /&gt;
#Finally, we recommend using a ssh public key (which will be downloaded at the end of the set-up process). We recommend you change your “Username” to your andrew ID but you can leave it as “azureuser” if you’d like (we recommend such things since you may forget and it&#039;s natural to have it consistent across your servers).&lt;br /&gt;
&lt;br /&gt;
[[File:1400px-Azure basics 1.png|frameless|700px|setting up new server, basic page (1)]]&lt;br /&gt;
&lt;br /&gt;
[[File:1400px-Azure basics 2.png|frameless|700px|setting up new server, basic page (2)]]&lt;br /&gt;
&lt;br /&gt;
==== Disks ====&lt;br /&gt;
&lt;br /&gt;
The second pages is about “Disks” although creating storage on Azure is complicated it’s a very necessary procedure. Click “Create and attache a new disk”. You will then have to select the right size. Generally, disks smaller than 512 GB are cheap for the department but 1 TB + are more costly. You personal computer likely has between 128 - 512 GB (maybe 1TB), but it’s really unlikely you’ll need too much for your project.&lt;br /&gt;
&lt;br /&gt;
[[File:1400px-Azure disk creation.png|frameless|700px|creating a disk]]&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
==== Network and Management ====&lt;br /&gt;
&lt;br /&gt;
There isn’t really anything necessary for you to do on these tabs.&lt;br /&gt;
&lt;br /&gt;
==== Advanced ====&lt;br /&gt;
&lt;br /&gt;
If you are using GPUs you’ll need to add the NVDIA extension for GPUs.&lt;br /&gt;
&lt;br /&gt;
==== Tags are also not necessary ====&lt;br /&gt;
&lt;br /&gt;
==== Done? ====&lt;br /&gt;
&lt;br /&gt;
You did it! Save your ssh key (&amp;lt;code&amp;gt;.pem&amp;lt;/code&amp;gt; file) in a location where you plan to call the server from. Also note that you will &#039;&#039;&#039;still&#039;&#039;&#039; need to correctly link your data disk you just created (which is discussed in the next step).&lt;br /&gt;
&lt;br /&gt;
=== Connecting your data disk ===&lt;br /&gt;
&lt;br /&gt;
This process is slightly complex (and can have implications if you don’t do it correctly). We recommend you follow the [https://docs.microsoft.com/en-us/azure/virtual-machines/linux/attach-disk-portal azure docs].&lt;br /&gt;
&lt;br /&gt;
==== step 1: finding and identifying your disk ====&lt;br /&gt;
&lt;br /&gt;
on the server run something like:&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;lsblk -o NAME,HCTL,SIZE,MOUNTPOINT | grep -i &amp;quot;sd&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
you should see a list something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;sda     3:0:0:0      128G&lt;br /&gt;
sdb     0:0:0:0      150G&lt;br /&gt;
├─sdb1             149.9G /&lt;br /&gt;
├─sdb14                4M&lt;br /&gt;
└─sdb15              106M /boot/efi&lt;br /&gt;
sdc     1:0:1:0      176G&lt;br /&gt;
└─sdc1               176G /mnt&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
now, on portal for the same VM, look at your disks (left side: Settings &amp;gt; Disks) and examine the size of your disk you added (for mine it’s 128G). As such we can conclude that my disk that needs mounting is &amp;lt;code&amp;gt;sda&amp;lt;/code&amp;gt; (this might not be the same for you - and is not the same in the azure docs).&lt;br /&gt;
&lt;br /&gt;
==== step 2: partitioning a new disk ====&lt;br /&gt;
&lt;br /&gt;
Copied form the azure docs (but changed for my disk name)&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;sudo parted /dev/sda --script mklabel gpt mkpart xfspart xfs 0% 100%&lt;br /&gt;
sudo mkfs.xfs /dev/sda1&lt;br /&gt;
sudo partprobe /dev/sda1&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== step 3: mount the disk ====&lt;br /&gt;
&lt;br /&gt;
Create a directory to mount the file system using mkdir. The following example creates a directory at &amp;lt;code&amp;gt;/datadrive&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;sudo mkdir /datadrive&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Use mount to then mount the filesystem. The following example mounts the &amp;lt;code&amp;gt;/dev/sda1&amp;lt;/code&amp;gt; partition to the &amp;lt;code&amp;gt;/datadrive&amp;lt;/code&amp;gt; mount point:&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;sudo mount /dev/sda1 /datadrive&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;Now we have&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;lsblk -o NAME,HCTL,SIZE,MOUNTPOINT | grep -i &amp;quot;sd&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;sda     3:0:0:0      128G&lt;br /&gt;
└─sda1               128G /datadrive&lt;br /&gt;
sdb     0:0:0:0      150G&lt;br /&gt;
├─sdb1             149.9G /&lt;br /&gt;
├─sdb14                4M&lt;br /&gt;
└─sdb15              106M /boot/efi&lt;br /&gt;
sdc     1:0:1:0      176G&lt;br /&gt;
└─sdc1               176G /mnt&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== step 4: … ====&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;sudo blkid&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;/dev/sdb1: LABEL=&amp;quot;cloudimg-rootfs&amp;quot; UUID=&amp;quot;1fe704c3-9cec-4cb5-b634-2e46f6bd9f90&amp;quot; TYPE=&amp;quot;ext4&amp;quot; PARTUUID=&amp;quot;318ec118-9248-4203-9cd5-7cb8c36f04d6&amp;quot;&lt;br /&gt;
/dev/sdb15: LABEL=&amp;quot;UEFI&amp;quot; UUID=&amp;quot;52A4-029B&amp;quot; TYPE=&amp;quot;vfat&amp;quot; PARTUUID=&amp;quot;2332d2b4-222e-425d-866e-227b42f3979d&amp;quot;&lt;br /&gt;
/dev/sdc1: UUID=&amp;quot;9cc09cc7-c65d-4fe4-a397-75679a438c1c&amp;quot; TYPE=&amp;quot;ext4&amp;quot; PARTUUID=&amp;quot;2b584de2-01&amp;quot;&lt;br /&gt;
/dev/sda1: UUID=&amp;quot;a2ba271d-e5a4-4416-84d2-b9408864b53b&amp;quot; TYPE=&amp;quot;xfs&amp;quot; PARTLABEL=&amp;quot;xfspart&amp;quot; PARTUUID=&amp;quot;77452866-0b43-4485-9956-673377498608&amp;quot;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== step 5: creating alias link to datadrive ====&lt;br /&gt;
&lt;br /&gt;
To create a symbolic link to your a &amp;lt;code&amp;gt;workspace&amp;lt;/code&amp;gt; folder to the &amp;lt;code&amp;gt;/datadrive&amp;lt;/code&amp;gt; you can do the following.&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;ln -s /datadrive ~/workspace &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Afterwards, everything in the &amp;lt;code&amp;gt;workspace&amp;lt;/code&amp;gt; will actually be stored on your new &amp;lt;code&amp;gt;/datadrive&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== step 6: making sure you have correct permissions ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ls -al /&amp;lt;/code&amp;gt; (might show you don’t have access (write, read, execute) to the /datadrive), so we did&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;sudo chmod 777 datadrive&amp;lt;/code&amp;gt; …&lt;br /&gt;
&lt;br /&gt;
[https://www.hostinger.com/tutorials/how-to-create-symbolic-links-in-linux/ https://www.hostinger.com/tutorials/how-to-create-symbolic-links-in-linux/]&lt;br /&gt;
&lt;br /&gt;
=== Connecting to your server and moving files ===&lt;br /&gt;
&lt;br /&gt;
To connect to your server you will need to know (1) where you&#039;re ssh key is (often called &amp;lt;code&amp;gt;_server_name_.pem&amp;lt;/code&amp;gt; (this should have been downloaded at the end of the creation of your server). &#039;&#039;&#039;Note that the first time your connect you need to make sure you have the correct permissions - see the connect page for the correct &amp;lt;code&amp;gt;cmod&amp;lt;/code&amp;gt; command you need to use!&#039;&#039;&#039; (2) your username defined in the [[Azure_getting_started#Basics|Basics]] step&lt;br /&gt;
&lt;br /&gt;
You will also need to know what IP address your server is currently using. This can before on the connect page of your server (as seen in the second image in this article). Then you&#039;re type something like&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
ssh -i _my_ssh_key_.pem _user_name_@_IP_address_ &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To pass files to and from your server with &amp;lt;code&amp;gt;scp&amp;lt;/code&amp;gt;, we will imagine you are on your home computer. Then do as follows:&lt;br /&gt;
&lt;br /&gt;
#&#039;&#039;&#039;server to local&#039;&#039;&#039; &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
scp -i _my_ssh_key_.pem _user_name_@_IP_address_:/full/path/to/files/on/server/my_script.R /local/path/to/files/on/local/computer/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#&#039;&#039;&#039;local to server&#039;&#039;&#039; &amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
scp -i _my_ssh_key_.pem /local/path/to/files/on/local/computer/my_script.R _user_name_@_IP_address_:/full/path/to/files/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To pass directories (and do the full thing you&#039;ll need to do the same as above but add&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
scp -i _my_ssh_key.pem -r ...&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Main_Page&amp;diff=269</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Main_Page&amp;diff=269"/>
		<updated>2025-07-29T16:22:02Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: Created an archive section and moved azure there&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Anyone can read this wiki. &amp;amp;nbsp;If you want to write/edit stuff in the wiki, you will need to log in with your account provided by the Statistics &amp;amp; Data Science Department computing group. This Wiki is intended to be a repository for instruction, information and ideas related to the many computing environments available to members and affiliates of the Statistics &amp;amp; Data Science Department.&lt;br /&gt;
&lt;br /&gt;
== Getting to know the computing resources &amp;amp; getting support ==&lt;br /&gt;
&lt;br /&gt;
If you&#039;re interested in knowing about what resources you have access to (and/or) what resources you have access to that might solve a problem you&#039;re having, please visit our [[What_resources_when|&amp;quot;what resources when&amp;quot; article]].&lt;br /&gt;
&lt;br /&gt;
To get support that is beyond this website please attend office hours below and/or email [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu] for support (for more details about how the support ticketing works through [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu] + ServiceNow please see the [[Service_now|ServiceNow]] article).&lt;br /&gt;
&lt;br /&gt;
Reset your StatDS password here - https://util.stat.cmu.edu/passwordreset/&lt;br /&gt;
&lt;br /&gt;
== Department Contacts ==&lt;br /&gt;
&lt;br /&gt;
• [mailto:events@stat.cmu.edu events@stat.cmu.edu]: seminars, conferences &amp;amp; workshops, and calendar updates&lt;br /&gt;
&lt;br /&gt;
• [mailto:news@stat.cmu.edu news@stat.cmu.edu]: dept/dept member-related public news &amp;amp; announcements&lt;br /&gt;
&lt;br /&gt;
• [mailto:lists@stat.cmu.edu lists@stat.cmu.edu]: department email lists&lt;br /&gt;
&lt;br /&gt;
• [mailto:webmaster@stat.cmu.edu webmaster@stat.cmu.edu]: website updates&lt;br /&gt;
&lt;br /&gt;
• [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu]: computer/tech requests&lt;br /&gt;
&lt;br /&gt;
• [mailto:rooms@stat.cmu.edu rooms@stat.cmu.edu]: request space for meetings and TA office hours&lt;br /&gt;
&lt;br /&gt;
• [mailto:reimbursements@stat.cmu.edu reimbursements@stat.cmu.edu]: travel and reimbursement requests&lt;br /&gt;
&lt;br /&gt;
== Computing Office Hours ==&lt;br /&gt;
&lt;br /&gt;
These office hour opportunities provide a dedicated time for you to come ask questions about research computing.&amp;amp;nbsp; These include code advice, how to access certain resources, what the best resources are for your project, or to learn about new connections to help you progress in your education and research. Staff Office hour times will be sent out each week on Monday as a calendar invite; accept any of the invitations to add it to your personal calendar. PhD TA Office Hours are below, unless a different time is&amp;amp;nbsp;communicated.&lt;br /&gt;
&lt;br /&gt;
#PhD TA Office Hours for the first half of summer 2025 are held by Elizabeth Cucuzzella in FMS Room 320 on Mondays from 2:30 pm to 4:30 pm. PhD students can also message on the #computing channel on slack.&lt;br /&gt;
#Staff Office Hours are held by appointment by Carl Skipper in BH 129G.  Schedule via [mailto:lists@stat.cmu.edu email].&lt;br /&gt;
&lt;br /&gt;
See [[Computing_office_hour_usage|computing office hour usage]] for what office hours can be used for.&lt;br /&gt;
&lt;br /&gt;
== Computing Wiki Pages ==&lt;br /&gt;
&lt;br /&gt;
=== Managing servers and environments ===&lt;br /&gt;
&lt;br /&gt;
==== Introductory Tutorials ====&lt;br /&gt;
[[Basic Linux Commands|Introduction to Basic Linux Commands]] &lt;br /&gt;
&lt;br /&gt;
==== Virtual environments ====&lt;br /&gt;
*[[Virtual Environment Introduction|Why use virtual environments?]]&lt;br /&gt;
*R&#039;s [[Renv]]&lt;br /&gt;
*Python&#039;s [[Python_virtual_environments|virtual environments (2 options)]]&lt;br /&gt;
*Old instructions for [[Conda with Python (Reference Only)|conda with Python]]&lt;br /&gt;
&lt;br /&gt;
==== Containers ====&lt;br /&gt;
*[[Introduction to containers]]&lt;br /&gt;
*[[Docker, and why-you-should-use-it-for-your-research]]&lt;br /&gt;
*[[Tutorials|Basic tutorial]]&lt;br /&gt;
*[[Running Docker locally and on Hydras]]&lt;br /&gt;
&lt;br /&gt;
=== Department computing resources ===&lt;br /&gt;
&lt;br /&gt;
==== Printing ====&lt;br /&gt;
*[[Department_Printers|Department Printers]] &lt;br /&gt;
&lt;br /&gt;
==== Web Hosting ====&lt;br /&gt;
#[[How to host your webpage under stat.cmu.edu]]&lt;br /&gt;
#[[Bulding your website with Hugo]]&lt;br /&gt;
&lt;br /&gt;
==== Servers ====&lt;br /&gt;
*[[Linux_and_department_servers|Department Servers]]. This wiki introduces you to the &#039;&#039;&#039;hydra&#039;&#039;&#039; servers, basic Linux commands, and more. &lt;br /&gt;
*#[[Terminal_multiplexers|Terminal multiplexers (screen and tmux)]]. This wiki presents tools for use on the &#039;&#039;hydra&#039;&#039; that can allow you to run jobs even when your not on the server, and much much more!&lt;br /&gt;
*#[[Python on the server]]. This wiki introduces uses command line approaches to running &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt; on a virtual machine, and how to use jupyter notebooks through the server as well.&lt;br /&gt;
*#[[R on the server]]. This wiki introduces uses command line approaches to running &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; on a virtual machine&lt;br /&gt;
&lt;br /&gt;
*[[Department GPU Servers]], for small jobs needing 1 GPU.&lt;br /&gt;
*[[GPU Cluster]], for larger jobs that may need multiple GPUs and many CPUs.&lt;br /&gt;
*[[PSC]], the Pittsburgh Supercomputing Center. Free accounts and usage credits are available &lt;br /&gt;
*#[[ACCESS]]&lt;br /&gt;
*#[[Slurm]]. This wiki introduces the job management system uses at PSC.&lt;br /&gt;
*#[[Parallelization_on_PSC/Bridges-2]]&lt;br /&gt;
*#[[Access PSC compute nodes directly from VSCode]]&lt;br /&gt;
*[[Open OnDemand]]&lt;br /&gt;
&lt;br /&gt;
=== Development guides ===&lt;br /&gt;
&lt;br /&gt;
====[[Parallel_computing|Parallelization]]====&lt;br /&gt;
*[[Parallelization_in_r|R-specific parallelization]]&lt;br /&gt;
*[[Parallelization_in_python|Python-specific parallelization]]&lt;br /&gt;
&lt;br /&gt;
==== Package development ====&lt;br /&gt;
*[[Developing_R_packages|R package development]]&lt;br /&gt;
*[[Developing_python_packages|Python package development]]&lt;br /&gt;
[[Terminal automation]]. This wiki introduces approaches to automate Linux/OS command line calls.&lt;br /&gt;
*[[Bash files]]. This wiki presents potential uses of Bash files including automated file/folder creation, parallelization and much more!&lt;br /&gt;
*[[Make]]. This wiki presents potential uses of &amp;lt;code&amp;gt;make&amp;lt;/code&amp;gt; and Makefiles including automated workflow and dependency tracking&lt;br /&gt;
*[[Jumping to frequently used folders and automating environment setups]]&lt;br /&gt;
&lt;br /&gt;
==== Version Control ====&lt;br /&gt;
See the guide provided in the site of [https://36-750.github.io/tools/version-control/ 36750 - Statistical Computing]&lt;br /&gt;
&lt;br /&gt;
==== Reference management ====&lt;br /&gt;
#Zotero &lt;br /&gt;
#Mendeley &lt;br /&gt;
&lt;br /&gt;
==== Integrated Development Environments (IDEs) ====&lt;br /&gt;
#[[VSCode|VSCode]] &lt;br /&gt;
#PyCharm &lt;br /&gt;
#RStudio &lt;br /&gt;
#Eclipse &lt;br /&gt;
&lt;br /&gt;
==== Text Editors ====&lt;br /&gt;
#Vim &lt;br /&gt;
#Nano &lt;br /&gt;
#Emacs &lt;br /&gt;
#Atom &lt;br /&gt;
#Sublime &lt;br /&gt;
&lt;br /&gt;
See also the guide provided in the site of [https://36-750.github.io/tools/editors/ 36750 - Statistical Computing]&lt;br /&gt;
&lt;br /&gt;
==== AI Tools ====&lt;br /&gt;
* [[ChatGPT and other LLMs]]&lt;br /&gt;
&lt;br /&gt;
*[[github_copilot|GitHub Copilot]]&lt;br /&gt;
&lt;br /&gt;
==== Miscellaneous====&lt;br /&gt;
#[[Windows_Subsystem_for_Linux_(WSL)_and_Python|Windows&amp;amp;nbsp;Subsystem for Linux (WSL) and Python]]&lt;br /&gt;
#[[Amphetamine|Amphetamine: How to keep your computer awake]]&lt;br /&gt;
&lt;br /&gt;
===General Department Computing Resources===&lt;br /&gt;
&lt;br /&gt;
*Fall 2021 [[Computing_seminar_resources|Computing Seminar Resources]] (video and slides)&lt;br /&gt;
&lt;br /&gt;
==Ideas for the Wiki==&lt;br /&gt;
&lt;br /&gt;
Please see the [[TODO|TODO page]] to post suggestions and errata.&lt;br /&gt;
&lt;br /&gt;
==Using the Wiki==&lt;br /&gt;
&lt;br /&gt;
*Here is some&amp;amp;nbsp;[[Generic_Wiki_Information|Generic Wiki Information]]&amp;amp;nbsp;that you may be interested in, such as a users&#039; guide and other resources for Wiki users.&lt;br /&gt;
&lt;br /&gt;
== Archived Pages ==&lt;br /&gt;
These area pages that are potentially useful for some users, but no longer reflect the computing resources of the department writ large.&lt;br /&gt;
&lt;br /&gt;
Azure&lt;br /&gt;
#[[Azure_getting_started|Azure: getting started]]. This wiki walks you through the steps to create a virtual machine through Azure&lt;br /&gt;
#[[How to obtain a virtual machine with a GPU]]&lt;br /&gt;
#[[Attach a disk to a virtual machine]]&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Open_OnDemand&amp;diff=268</id>
		<title>Open OnDemand</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Open_OnDemand&amp;diff=268"/>
		<updated>2025-07-29T15:20:26Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: /* Getting Started */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Open OnDemand provides easy, remote web access to supercomputers and computing clusters, without the need for a command-line interface. Instead of accessing the hydra and ghidorah clusters via your terminal, Open OnDemand provides a user interface for accessing and modifying files and submitting jobs. No installation is required -- everything can be accessed within the browser.&lt;br /&gt;
&lt;br /&gt;
=== Getting Started ===&lt;br /&gt;
To access Open OnDemand, login [https://ood.stat.cmu.edu/dex/auth/ldap/login?back=&amp;amp;state=iqxvp3cbpitdiomazu5eih4nc here] with your StatDS username and password. This is the same username and password you would use to access the clusters via ssh on your terminal.&lt;br /&gt;
&lt;br /&gt;
After logging in, you should be redirected to your file system. You can navigate through your directories just as you would on your desktop. A nice feature of Open OnDemand is you can automatically upload and download files from your desktop to the department&#039;s file system; the secure file transfer is done automatically in a user-friendly manner.&lt;br /&gt;
&lt;br /&gt;
=== Editing Files ===&lt;br /&gt;
To create a new file or directory, simply click &amp;quot;New File&amp;quot; or &amp;quot;New Directory&amp;quot; in the upper right hand corner of the screen.&lt;br /&gt;
&lt;br /&gt;
To edit existing files, navigate to the file in the file system. Click on the dropdown three vertical dots to the right of the file&#039;s name. An options list will appear that includes a &amp;quot;View&amp;quot; mode and an &amp;quot;Edit&amp;quot; mode. Clicking view will allow you to view the contents of the file without manipulating them. Edit will redirect you to another tab where you can directly edit the contents of the file. Click the &amp;quot;Save&amp;quot; button on the lefthand side of the screen to save edits; when this button is grayed out, it means there are no additional changes to save.&lt;br /&gt;
&lt;br /&gt;
=== Accessing the Clusters ===&lt;br /&gt;
To access the head node via the terminal, navigate to the &amp;quot;Clusters&amp;quot; drop down in the top left of the screen, and select &amp;quot;Statistics and Data Science Shell Access.&amp;quot; The shell allows you to access your files within a terminal on the head compute node. Do not run jobs within this terminal. Beneath the cluster selection, you can view the system status which tells you how many nodes, processors, and GPUs are currently available, as well as how many jobs are running and queued. This will help inform how long a job you&#039;ve created might take to run on the system/when it will begin running.&lt;br /&gt;
&lt;br /&gt;
=== Submitting and Tracking Jobs ===&lt;br /&gt;
&lt;br /&gt;
==== Default Template ====&lt;br /&gt;
To submit or compose a job, navigate to the &amp;quot;Jobs&amp;quot; drop down in the top left of the screen, and select &amp;quot;Job Composer.&amp;quot; This will take you to the job composer page, where you can select the &amp;quot;New Job&amp;quot; drop down. To start from the default template, just click &amp;quot;From Default Template&amp;quot; and in the right hand panel, scroll down to where the .sh script is shone and click &amp;quot;Open Editor&amp;quot; to edit the script itself. Once you are ready to submit the job, simply click the job in your list of potential jobs, and click submit. The job will show a status of &amp;quot;Queued,&amp;quot; &amp;quot;Running,&amp;quot; or &amp;quot;Completed&amp;quot; and you can view output files in the right hand panel once it has completed running.&lt;br /&gt;
&lt;br /&gt;
==== Hydras and Ghidorah Templates ====&lt;br /&gt;
If you click &amp;quot;From Template,&amp;quot; you can choose to submit a job on the hydras or ghidorahs. Simply pick the template you want to use -- either Hydra Job for the hydras or Ghidorah Job for the ghidorahs -- and click &amp;quot;Create New Job.&amp;quot; You can then modify the submit.sh script as before to select &#039;&#039;which&#039;&#039; hydra or ghidorah you want to use as follows:&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
#SBATCH --partition Ghidorahs&lt;br /&gt;
&lt;br /&gt;
# #Select a specific Hydra node to run job on.&lt;br /&gt;
# #Un-comment the line below and replace ghidorahXX with the appropriate ghidorah server ~ ghidorah1 in this case.&lt;br /&gt;
#SBATCH --nodelist=ghidorah1&lt;br /&gt;
&lt;br /&gt;
# #Number of tasks.&lt;br /&gt;
#SBATCH --ntasks 1&lt;br /&gt;
&lt;br /&gt;
# #Number of CPUs per tasks.&lt;br /&gt;
#SBATCH --cpus-per-task 1&lt;br /&gt;
&lt;br /&gt;
# #Amount of RAM&lt;br /&gt;
#SBATCH --mem=4G&lt;br /&gt;
&lt;br /&gt;
# #Output file for script.&lt;br /&gt;
#SBATCH --output=%j_output.txt&lt;br /&gt;
&lt;br /&gt;
# #Error file for script.&lt;br /&gt;
#SBATCH --error=%j_error.txt&lt;br /&gt;
&lt;br /&gt;
python3 examplepython.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;The &amp;lt;code&amp;gt;#SBATCH --nodelist=ghidorah1&amp;lt;/code&amp;gt; selects the 1st ghidorah to run the python script on. Change this to &amp;lt;code&amp;gt;#SBATCH --nodelist=hydra1&amp;lt;/code&amp;gt; for the hydras and change the first SBATCH line to Hydras (&amp;lt;code&amp;gt;#SBATCH --partition Hydras&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
==== Submitting a Script from a Specified Path ====&lt;br /&gt;
You can also specify a path to a job you have already created. Click &amp;quot;From Specified Path&amp;quot; and enter the source path, i.e. the path to the directory containing your .sh file. Once you have done that, you can specify the name of the script under Script name, i.e. the name of the .sh file in the directory you&#039;ve navigated to. You can then click &amp;quot;Run&amp;quot; just as you have with your other jobs.&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Open_OnDemand&amp;diff=238</id>
		<title>Open OnDemand</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Open_OnDemand&amp;diff=238"/>
		<updated>2025-07-16T15:04:11Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Open OnDemand provides easy, remote web access to supercomputers and computing clusters, without the need for a command-line interface. Instead of accessing the hydra and ghidorah clusters via your terminal, Open OnDemand provides a user interface for accessing and modifying files and submitting jobs. No installation is required -- everything can be accessed within the browser.&lt;br /&gt;
&lt;br /&gt;
=== Getting Started ===&lt;br /&gt;
To access Open OnDemand, login [https://ood.stat.cmu.edu/dex/auth/ldap/login?back=&amp;amp;state=oiswjnltgm3e46hgcld2kyple here] with your StatDS username and password. This is the same username and password you would use to access the clusters via ssh on your terminal.&lt;br /&gt;
&lt;br /&gt;
After logging in, you should be redirected to your file system. You can navigate through your directories just as you would on your desktop. A nice feature of Open OnDemand is you can automatically upload and download files from your desktop to the department&#039;s file system; the secure file transfer is done automatically in a user-friendly manner.&lt;br /&gt;
&lt;br /&gt;
=== Editing Files ===&lt;br /&gt;
To create a new file or directory, simply click &amp;quot;New File&amp;quot; or &amp;quot;New Directory&amp;quot; in the upper right hand corner of the screen.&lt;br /&gt;
&lt;br /&gt;
To edit existing files, navigate to the file in the file system. Click on the dropdown three vertical dots to the right of the file&#039;s name. An options list will appear that includes a &amp;quot;View&amp;quot; mode and an &amp;quot;Edit&amp;quot; mode. Clicking view will allow you to view the contents of the file without manipulating them. Edit will redirect you to another tab where you can directly edit the contents of the file. Click the &amp;quot;Save&amp;quot; button on the lefthand side of the screen to save edits; when this button is grayed out, it means there are no additional changes to save.&lt;br /&gt;
&lt;br /&gt;
=== Accessing the Clusters ===&lt;br /&gt;
To access the clusters via the terminal, navigate to the &amp;quot;Clusters&amp;quot; drop down in the top left of the screen, and select &amp;quot;Statistics and Data Science Shell Access.&amp;quot; The shell access is built for both the ghidorahs and the hydras. Beneath the cluster selection, you can view the system status which tells you how many nodes, processors, and GPUs are currently available, as well as how many jobs are running and queued. This will help inform how long a job you&#039;ve created might take to run on the system/when it will begin running.&lt;br /&gt;
&lt;br /&gt;
=== Submitting and Tracking Jobs ===&lt;br /&gt;
&lt;br /&gt;
==== Default Template ====&lt;br /&gt;
To submit or compose a job, navigate to the &amp;quot;Jobs&amp;quot; drop down in the top left of the screen, and select &amp;quot;Job Composer.&amp;quot; This will take you to the job composer page, where you can select the &amp;quot;New Job&amp;quot; drop down. To start from the default template, just click &amp;quot;From Default Template&amp;quot; and in the right hand panel, scroll down to where the .sh script is shone and click &amp;quot;Open Editor&amp;quot; to edit the script itself. Once you are ready to submit the job, simply click the job in your list of potential jobs, and click submit. The job will show a status of &amp;quot;Queued,&amp;quot; &amp;quot;Running,&amp;quot; or &amp;quot;Completed&amp;quot; and you can view output files in the right hand panel once it has completed running.&lt;br /&gt;
&lt;br /&gt;
==== Hydras and Ghidorah Templates ====&lt;br /&gt;
If you click &amp;quot;From Template,&amp;quot; you can choose to submit a job on the hydras or ghidorahs. Simply pick the template you want to use -- either Hydra Job for the hydras or Ghidorah Job for the ghidorahs -- and click &amp;quot;Create New Job.&amp;quot; You can then modify the submit.sh script as before to select &#039;&#039;which&#039;&#039; hydra or ghidorah you want to use as follows:&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/sh&lt;br /&gt;
#SBATCH --partition Ghidorahs&lt;br /&gt;
&lt;br /&gt;
# #Select a specific Hydra node to run job on.&lt;br /&gt;
# #Un-comment the line below and replace ghidorahXX with the appropriate ghidorah server ~ ghidorah1 in this case.&lt;br /&gt;
#SBATCH --nodelist=ghidorah1&lt;br /&gt;
&lt;br /&gt;
# #Number of tasks.&lt;br /&gt;
#SBATCH --ntasks 1&lt;br /&gt;
&lt;br /&gt;
# #Number of CPUs per tasks.&lt;br /&gt;
#SBATCH --cpus-per-task 1&lt;br /&gt;
&lt;br /&gt;
# #Amount of RAM&lt;br /&gt;
#SBATCH --mem=4G&lt;br /&gt;
&lt;br /&gt;
# #Output file for script.&lt;br /&gt;
#SBATCH --output=%j_output.txt&lt;br /&gt;
&lt;br /&gt;
# #Error file for script.&lt;br /&gt;
#SBATCH --error=%j_error.txt&lt;br /&gt;
&lt;br /&gt;
python3 examplepython.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;The &amp;lt;code&amp;gt;#SBATCH --nodelist=ghidorah1&amp;lt;/code&amp;gt; selects the 1st ghidorah to run the python script on. Change this to &amp;lt;code&amp;gt;#SBATCH --nodelist=hydra1&amp;lt;/code&amp;gt; for the hydras and change the first SBATCH line to Hydras (&amp;lt;code&amp;gt;#SBATCH --partition Hydras&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
==== Submitting a Script from a Specified Path ====&lt;br /&gt;
You can also specify a path to a job you have already created. Click &amp;quot;From Specified Path&amp;quot; and enter the source path, i.e. the path to the directory containing your .sh file. Once you have done that, you can specify the name of the script under Script name, i.e. the name of the .sh file in the directory you&#039;ve navigated to. You can then click &amp;quot;Run&amp;quot; just as you have with your other jobs.&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Open_OnDemand&amp;diff=237</id>
		<title>Open OnDemand</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Open_OnDemand&amp;diff=237"/>
		<updated>2025-07-16T14:53:27Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: Created page with &amp;quot;Open OnDemand provides easy, remote web access to supercomputers and computing clusters, without the need for a command-line interface. Instead of accessing the hydra and ghid...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Open OnDemand provides easy, remote web access to supercomputers and computing clusters, without the need for a command-line interface. Instead of accessing the hydra and ghidorah clusters via your terminal, Open OnDemand provides a user interface for accessing and modifying files and submitting jobs. No installation is required -- everything can be accessed within the browser.&lt;br /&gt;
&lt;br /&gt;
=== Getting Started ===&lt;br /&gt;
To access Open OnDemand, login [https://ood.stat.cmu.edu/dex/auth/ldap/login?back=&amp;amp;state=oiswjnltgm3e46hgcld2kyple here] with your StatDS username and password. This is the same username and password you would use to access the clusters via ssh on your terminal.&lt;br /&gt;
&lt;br /&gt;
After logging in, you should be redirected to your file system. You can navigate through your directories just as you would on your desktop. A nice feature of Open OnDemand is you can automatically upload and download files from your desktop to the department&#039;s file system; the secure file transfer is done automatically in a user-friendly manner.&lt;br /&gt;
&lt;br /&gt;
=== Editing Files ===&lt;br /&gt;
To create a new file or directory, simply click &amp;quot;New File&amp;quot; or &amp;quot;New Directory&amp;quot; in the upper right hand corner of the screen.&lt;br /&gt;
&lt;br /&gt;
To edit existing files, navigate to the file in the file system. Click on the dropdown three vertical dots to the right of the file&#039;s name. An options list will appear that includes a &amp;quot;View&amp;quot; mode and an &amp;quot;Edit&amp;quot; mode. Clicking view will allow you to view the contents of the file without manipulating them. Edit will redirect you to another tab where you can directly edit the contents of the file. Click the &amp;quot;Save&amp;quot; button on the lefthand side of the screen to save edits; when this button is grayed out, it means there are no additional changes to save.&lt;br /&gt;
&lt;br /&gt;
=== Accessing the Clusters ===&lt;br /&gt;
To access the clusters via the terminal, navigate to the &amp;quot;Clusters&amp;quot; drop down in the top left of the screen, and select &amp;quot;Statistics and Data Science Shell Access.&amp;quot; The shell access is built for both the ghidorahs and the hydras. Beneath the cluster selection, you can view the system status which tells you how many nodes, processors, and GPUs are currently available, as well as how many jobs are running and queued. This will help inform how long a job you&#039;ve created might take to run on the system/when it will begin running.&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Main_Page&amp;diff=232</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Main_Page&amp;diff=232"/>
		<updated>2025-07-16T13:47:53Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Anyone can read this wiki. &amp;amp;nbsp;If you want to write/edit stuff in the wiki, you will need to log in with your account provided by the Statistics &amp;amp; Data Science Department computing group. This Wiki is intended to be a repository for instruction, information and ideas related to the many computing environments available to members and affiliates of the Statistics &amp;amp; Data Science Department.&lt;br /&gt;
&lt;br /&gt;
== Getting to know the computing resources &amp;amp; getting support ==&lt;br /&gt;
&lt;br /&gt;
If you&#039;re interested in knowing about what resources you have access to (and/or) what resources you have access to that might solve a problem you&#039;re having, please visit our [[What_resources_when|&amp;quot;what resources when&amp;quot; article]].&lt;br /&gt;
&lt;br /&gt;
To get support that is beyond this website please attend office hours below and/or email [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu] for support (for more details about how the support ticketing works through [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu] + ServiceNow please see the [[Service_now|ServiceNow]] article).&lt;br /&gt;
&lt;br /&gt;
Reset your StatDS password here - https://util.stat.cmu.edu/passwordreset/&lt;br /&gt;
&lt;br /&gt;
== Department Contacts ==&lt;br /&gt;
&lt;br /&gt;
• [mailto:events@stat.cmu.edu events@stat.cmu.edu]: seminars, conferences &amp;amp; workshops, and calendar updates&lt;br /&gt;
&lt;br /&gt;
• [mailto:news@stat.cmu.edu news@stat.cmu.edu]: dept/dept member-related public news &amp;amp; announcements&lt;br /&gt;
&lt;br /&gt;
• [mailto:lists@stat.cmu.edu lists@stat.cmu.edu]: department email lists&lt;br /&gt;
&lt;br /&gt;
• [mailto:webmaster@stat.cmu.edu webmaster@stat.cmu.edu]: website updates&lt;br /&gt;
&lt;br /&gt;
• [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu]: computer/tech requests&lt;br /&gt;
&lt;br /&gt;
• [mailto:rooms@stat.cmu.edu rooms@stat.cmu.edu]: request space for meetings and TA office hours&lt;br /&gt;
&lt;br /&gt;
• [mailto:reimbursements@stat.cmu.edu reimbursements@stat.cmu.edu]: travel and reimbursement requests&lt;br /&gt;
&lt;br /&gt;
== Computing Office Hours ==&lt;br /&gt;
&lt;br /&gt;
These office hour opportunities provide a dedicated time for you to come ask questions about research computing.&amp;amp;nbsp; These include code advice, how to access certain resources, what the best resources are for your project, or to learn about new connections to help you progress in your education and research. Staff Office hour times will be sent out each week on Monday as a calendar invite; accept any of the invitations to add it to your personal calendar. PhD TA Office Hours are below, unless a different time is&amp;amp;nbsp;communicated.&lt;br /&gt;
&lt;br /&gt;
#PhD TA Office Hours for the first half of summer 2025 are held by Elizabeth Cucuzzella in FMS Room 320 on Mondays from 2:30 pm to 4:30 pm. PhD students can also message on the #computing channel on slack.&lt;br /&gt;
#Staff Office Hours are held by appointment by Carl Skipper in BH 129G.  Schedule via [mailto:lists@stat.cmu.edu email].&lt;br /&gt;
&lt;br /&gt;
See [[Computing_office_hour_usage|computing office hour usage]] for what office hours can be used for.&lt;br /&gt;
&lt;br /&gt;
== Computing Wiki Pages ==&lt;br /&gt;
&lt;br /&gt;
=== Managing servers and environments ===&lt;br /&gt;
&lt;br /&gt;
==== Introductory Tutorials ====&lt;br /&gt;
[[Basic Linux Commands|Introduction to Basic Linux Commands]] &lt;br /&gt;
&lt;br /&gt;
==== Virtual environments ====&lt;br /&gt;
*[[Virtual Environment Introduction|Why use virtual environments?]]&lt;br /&gt;
*R&#039;s [[Renv]]&lt;br /&gt;
*Python&#039;s [[Python_virtual_environments|virtual environments (2 options)]]&lt;br /&gt;
*Old instructions for [[Conda with Python (Reference Only)|conda with Python]]&lt;br /&gt;
&lt;br /&gt;
==== Containers ====&lt;br /&gt;
*[[Introduction to containers]]&lt;br /&gt;
*[[Docker, and why-you-should-use-it-for-your-research]]&lt;br /&gt;
*[[Tutorials|Basic tutorial]]&lt;br /&gt;
*[[Running Docker locally and on Hydras]]&lt;br /&gt;
&lt;br /&gt;
=== Department computing resources ===&lt;br /&gt;
&lt;br /&gt;
==== Printing ====&lt;br /&gt;
*[[Department_Printers|Department Printers]] &lt;br /&gt;
&lt;br /&gt;
==== Web Hosting ====&lt;br /&gt;
#[[How to host your webpage under stat.cmu.edu]]&lt;br /&gt;
#[[Bulding your website with Hugo]]&lt;br /&gt;
&lt;br /&gt;
==== Servers ====&lt;br /&gt;
*[[Linux_and_department_servers|Department Servers]]. This wiki introduces you to the &#039;&#039;&#039;hydra&#039;&#039;&#039; servers, basic Linux commands, and more. &lt;br /&gt;
*#[[Terminal_multiplexers|Terminal multiplexers (screen and tmux)]]. This wiki presents tools for use on the &#039;&#039;hydra&#039;&#039; that can allow you to run jobs even when your not on the server, and much much more!&lt;br /&gt;
*#[[Python on the server]]. This wiki introduces uses command line approaches to running &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt; on a virtual machine, and how to use jupyter notebooks through the server as well.&lt;br /&gt;
*#[[R on the server]]. This wiki introduces uses command line approaches to running &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; on a virtual machine&lt;br /&gt;
&lt;br /&gt;
*[[Department GPU Servers]], for small jobs needing 1 GPU.&lt;br /&gt;
*[[GPU Cluster]], for larger jobs that may need multiple GPUs and many CPUs.&lt;br /&gt;
*[[PSC]], the Pittsburgh Supercomputing Center. Free accounts and usage credits are available &lt;br /&gt;
*#[[ACCESS]]&lt;br /&gt;
*#[[Slurm]]. This wiki introduces the job management system uses at PSC.&lt;br /&gt;
*#[[Parallelization_on_PSC/Bridges-2]]&lt;br /&gt;
*#[[Access PSC compute nodes directly from VSCode]]&lt;br /&gt;
*Azure&lt;br /&gt;
*#[[Azure_getting_started|Azure: getting started]]. This wiki walks you through the steps to create a virtual machine through Azure&lt;br /&gt;
*#[[How to obtain a virtual machine with a GPU]]&lt;br /&gt;
*#[[Attach a disk to a virtual machine]]&lt;br /&gt;
*[[Open OnDemand]]&lt;br /&gt;
&lt;br /&gt;
=== Development guides ===&lt;br /&gt;
&lt;br /&gt;
====[[Parallel_computing|Parallelization]]====&lt;br /&gt;
*[[Parallelization_in_r|R-specific parallelization]]&lt;br /&gt;
*[[Parallelization_in_python|Python-specific parallelization]]&lt;br /&gt;
&lt;br /&gt;
==== Package development ====&lt;br /&gt;
*[[Developing_R_packages|R package development]]&lt;br /&gt;
*[[Developing_python_packages|Python package development]]&lt;br /&gt;
[[Terminal automation]]. This wiki introduces approaches to automate Linux/OS command line calls.&lt;br /&gt;
*[[Bash files]]. This wiki presents potential uses of Bash files including automated file/folder creation, parallelization and much more!&lt;br /&gt;
*[[Make]]. This wiki presents potential uses of &amp;lt;code&amp;gt;make&amp;lt;/code&amp;gt; and Makefiles including automated workflow and dependency tracking&lt;br /&gt;
*[[Jumping to frequently used folders and automating environment setups]]&lt;br /&gt;
&lt;br /&gt;
==== Version Control ====&lt;br /&gt;
See the guide provided in the site of [https://36-750.github.io/tools/version-control/ 36750 - Statistical Computing]&lt;br /&gt;
&lt;br /&gt;
==== Reference management ====&lt;br /&gt;
#Zotero &lt;br /&gt;
#Mendeley &lt;br /&gt;
&lt;br /&gt;
==== Integrated Development Environments (IDEs) ====&lt;br /&gt;
#[[VSCode|VSCode]] &lt;br /&gt;
#PyCharm &lt;br /&gt;
#RStudio &lt;br /&gt;
#Eclipse &lt;br /&gt;
&lt;br /&gt;
==== Text Editors ====&lt;br /&gt;
#Vim &lt;br /&gt;
#Nano &lt;br /&gt;
#Emacs &lt;br /&gt;
#Atom &lt;br /&gt;
#Sublime &lt;br /&gt;
&lt;br /&gt;
See also the guide provided in the site of [https://36-750.github.io/tools/editors/ 36750 - Statistical Computing]&lt;br /&gt;
&lt;br /&gt;
==== AI Tools ====&lt;br /&gt;
* [[ChatGPT and other LLMs]]&lt;br /&gt;
&lt;br /&gt;
*[[github_copilot|GitHub Copilot]]&lt;br /&gt;
&lt;br /&gt;
==== Miscellaneous====&lt;br /&gt;
#[[Windows_Subsystem_for_Linux_(WSL)_and_Python|Windows&amp;amp;nbsp;Subsystem for Linux (WSL) and Python]]&lt;br /&gt;
#[[Amphetamine|Amphetamine: How to keep your computer awake]]&lt;br /&gt;
&lt;br /&gt;
===General Department Computing Resources===&lt;br /&gt;
&lt;br /&gt;
*Fall 2021 [[Computing_seminar_resources|Computing Seminar Resources]] (video and slides)&lt;br /&gt;
&lt;br /&gt;
==Ideas for the Wiki==&lt;br /&gt;
&lt;br /&gt;
Please see the [[TODO|TODO page]] to post suggestions and errata.&lt;br /&gt;
&lt;br /&gt;
==Using the Wiki==&lt;br /&gt;
&lt;br /&gt;
*Here is some&amp;amp;nbsp;[[Generic_Wiki_Information|Generic Wiki Information]]&amp;amp;nbsp;that you may be interested in, such as a users&#039; guide and other resources for Wiki users.&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Main_Page&amp;diff=231</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Main_Page&amp;diff=231"/>
		<updated>2025-07-11T14:31:24Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Anyone can read this wiki. &amp;amp;nbsp;If you want to write/edit stuff in the wiki, you will need to log in with your account provided by the Statistics &amp;amp; Data Science Department computing group. This Wiki is intended to be a repository for instruction, information and ideas related to the many computing environments available to members and affiliates of the Statistics &amp;amp; Data Science Department.&lt;br /&gt;
&lt;br /&gt;
== Getting to know the computing resources &amp;amp; getting support ==&lt;br /&gt;
&lt;br /&gt;
If you&#039;re interested in knowing about what resources you have access to (and/or) what resources you have access to that might solve a problem you&#039;re having, please visit our [[What_resources_when|&amp;quot;what resources when&amp;quot; article]].&lt;br /&gt;
&lt;br /&gt;
To get support that is beyond this website please attend office hours below and/or email [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu] for support (for more details about how the support ticketing works through [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu] + ServiceNow please see the [[Service_now|ServiceNow]] article).&lt;br /&gt;
&lt;br /&gt;
Reset your StatDS password here - https://util.stat.cmu.edu/passwordreset/&lt;br /&gt;
&lt;br /&gt;
== Department Contacts ==&lt;br /&gt;
&lt;br /&gt;
• [mailto:events@stat.cmu.edu events@stat.cmu.edu]: seminars, conferences &amp;amp; workshops, and calendar updates&lt;br /&gt;
&lt;br /&gt;
• [mailto:news@stat.cmu.edu news@stat.cmu.edu]: dept/dept member-related public news &amp;amp; announcements&lt;br /&gt;
&lt;br /&gt;
• [mailto:lists@stat.cmu.edu lists@stat.cmu.edu]: department email lists&lt;br /&gt;
&lt;br /&gt;
• [mailto:webmaster@stat.cmu.edu webmaster@stat.cmu.edu]: website updates&lt;br /&gt;
&lt;br /&gt;
• [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu]: computer/tech requests&lt;br /&gt;
&lt;br /&gt;
• [mailto:rooms@stat.cmu.edu rooms@stat.cmu.edu]: request space for meetings and TA office hours&lt;br /&gt;
&lt;br /&gt;
• [mailto:reimbursements@stat.cmu.edu reimbursements@stat.cmu.edu]: travel and reimbursement requests&lt;br /&gt;
&lt;br /&gt;
== Computing Office Hours ==&lt;br /&gt;
&lt;br /&gt;
These office hour opportunities provide a dedicated time for you to come ask questions about research computing.&amp;amp;nbsp; These include code advice, how to access certain resources, what the best resources are for your project, or to learn about new connections to help you progress in your education and research. Staff Office hour times will be sent out each week on Monday as a calendar invite; accept any of the invitations to add it to your personal calendar. PhD TA Office Hours are below, unless a different time is&amp;amp;nbsp;communicated.&lt;br /&gt;
&lt;br /&gt;
#PhD TA Office Hours for the first half of summer 2025 are held by Elizabeth Cucuzzella in FMS Room 320 on Mondays from 2:30 pm to 4:30 pm. PhD students can also message on the #computing channel on slack.&lt;br /&gt;
#Staff Office Hours are held by appointment by Carl Skipper in BH 129G.  Schedule via [mailto:lists@stat.cmu.edu email].&lt;br /&gt;
&lt;br /&gt;
See [[Computing_office_hour_usage|computing office hour usage]] for what office hours can be used for.&lt;br /&gt;
&lt;br /&gt;
== Computing Wiki Pages ==&lt;br /&gt;
&lt;br /&gt;
=== Managing servers and environments ===&lt;br /&gt;
&lt;br /&gt;
==== Introductory Tutorials ====&lt;br /&gt;
[[Basic Linux Commands|Introduction to Basic Linux Commands]] &lt;br /&gt;
&lt;br /&gt;
==== Virtual environments ====&lt;br /&gt;
*[[Virtual Environment Introduction|Why use virtual environments?]]&lt;br /&gt;
*R&#039;s [[Renv]]&lt;br /&gt;
*Python&#039;s [[Python_virtual_environments|virtual environments (2 options)]]&lt;br /&gt;
*Old instructions for [[Conda with Python (Reference Only)|conda with Python]]&lt;br /&gt;
&lt;br /&gt;
==== Containers ====&lt;br /&gt;
*[[Introduction to containers]]&lt;br /&gt;
*[[Docker, and why-you-should-use-it-for-your-research]]&lt;br /&gt;
*[[Tutorials|Basic tutorial]]&lt;br /&gt;
*[[Running Docker locally and on Hydras]]&lt;br /&gt;
&lt;br /&gt;
=== Department computing resources ===&lt;br /&gt;
&lt;br /&gt;
==== Printing ====&lt;br /&gt;
*[[Department_Printers|Department Printers]] &lt;br /&gt;
&lt;br /&gt;
==== Web Hosting ====&lt;br /&gt;
#[[How to host your webpage under stat.cmu.edu]]&lt;br /&gt;
#[[Bulding your website with Hugo]]&lt;br /&gt;
&lt;br /&gt;
==== Servers ====&lt;br /&gt;
*[[Linux_and_department_servers|Department Servers]]. This wiki introduces you to the &#039;&#039;&#039;hydra&#039;&#039;&#039; servers, basic Linux commands, and more. &lt;br /&gt;
*#[[Terminal_multiplexers|Terminal multiplexers (screen and tmux)]]. This wiki presents tools for use on the &#039;&#039;hydra&#039;&#039; that can allow you to run jobs even when your not on the server, and much much more!&lt;br /&gt;
*#[[Python on the server]]. This wiki introduces uses command line approaches to running &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt; on a virtual machine, and how to use jupyter notebooks through the server as well.&lt;br /&gt;
*#[[R on the server]]. This wiki introduces uses command line approaches to running &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; on a virtual machine&lt;br /&gt;
&lt;br /&gt;
*[[Department GPU Servers]], for small jobs needing 1 GPU.&lt;br /&gt;
*[[GPU Cluster]], for larger jobs that may need multiple GPUs and many CPUs.&lt;br /&gt;
*[[PSC]], the Pittsburgh Supercomputing Center. Free accounts and usage credits are available &lt;br /&gt;
*#[[ACCESS]]&lt;br /&gt;
*#[[Slurm]]. This wiki introduces the job management system uses at PSC.&lt;br /&gt;
*#[[Parallelization_on_PSC/Bridges-2]]&lt;br /&gt;
*#[[Access PSC compute nodes directly from VSCode]]&lt;br /&gt;
*Azure&lt;br /&gt;
*#[[Azure_getting_started|Azure: getting started]]. This wiki walks you through the steps to create a virtual machine through Azure&lt;br /&gt;
*#[[How to obtain a virtual machine with a GPU]]&lt;br /&gt;
*#[[Attach a disk to a virtual machine]]&lt;br /&gt;
&lt;br /&gt;
=== Development guides ===&lt;br /&gt;
&lt;br /&gt;
====[[Parallel_computing|Parallelization]]====&lt;br /&gt;
*[[Parallelization_in_r|R-specific parallelization]]&lt;br /&gt;
*[[Parallelization_in_python|Python-specific parallelization]]&lt;br /&gt;
&lt;br /&gt;
==== Package development ====&lt;br /&gt;
*[[Developing_R_packages|R package development]]&lt;br /&gt;
*[[Developing_python_packages|Python package development]]&lt;br /&gt;
[[Terminal automation]]. This wiki introduces approaches to automate Linux/OS command line calls.&lt;br /&gt;
*[[Bash files]]. This wiki presents potential uses of Bash files including automated file/folder creation, parallelization and much more!&lt;br /&gt;
*[[Make]]. This wiki presents potential uses of &amp;lt;code&amp;gt;make&amp;lt;/code&amp;gt; and Makefiles including automated workflow and dependency tracking&lt;br /&gt;
*[[Jumping to frequently used folders and automating environment setups]]&lt;br /&gt;
&lt;br /&gt;
==== Version Control ====&lt;br /&gt;
See the guide provided in the site of [https://36-750.github.io/tools/version-control/ 36750 - Statistical Computing]&lt;br /&gt;
&lt;br /&gt;
==== Reference management ====&lt;br /&gt;
#Zotero &lt;br /&gt;
#Mendeley &lt;br /&gt;
&lt;br /&gt;
==== Integrated Development Environments (IDEs) ====&lt;br /&gt;
#[[VSCode|VSCode]] &lt;br /&gt;
#PyCharm &lt;br /&gt;
#RStudio &lt;br /&gt;
#Eclipse &lt;br /&gt;
&lt;br /&gt;
==== Text Editors ====&lt;br /&gt;
#Vim &lt;br /&gt;
#Nano &lt;br /&gt;
#Emacs &lt;br /&gt;
#Atom &lt;br /&gt;
#Sublime &lt;br /&gt;
&lt;br /&gt;
See also the guide provided in the site of [https://36-750.github.io/tools/editors/ 36750 - Statistical Computing]&lt;br /&gt;
&lt;br /&gt;
==== AI Tools ====&lt;br /&gt;
* [[ChatGPT and other LLMs]]&lt;br /&gt;
&lt;br /&gt;
*[[github_copilot|GitHub Copilot]]&lt;br /&gt;
&lt;br /&gt;
==== Miscellaneous====&lt;br /&gt;
#[[Windows_Subsystem_for_Linux_(WSL)_and_Python|Windows&amp;amp;nbsp;Subsystem for Linux (WSL) and Python]]&lt;br /&gt;
#[[Amphetamine|Amphetamine: How to keep your computer awake]]&lt;br /&gt;
&lt;br /&gt;
===General Department Computing Resources===&lt;br /&gt;
&lt;br /&gt;
*Fall 2021 [[Computing_seminar_resources|Computing Seminar Resources]] (video and slides)&lt;br /&gt;
&lt;br /&gt;
==Ideas for the Wiki==&lt;br /&gt;
&lt;br /&gt;
Please see the [[TODO|TODO page]] to post suggestions and errata.&lt;br /&gt;
&lt;br /&gt;
==Using the Wiki==&lt;br /&gt;
&lt;br /&gt;
*Here is some&amp;amp;nbsp;[[Generic_Wiki_Information|Generic Wiki Information]]&amp;amp;nbsp;that you may be interested in, such as a users&#039; guide and other resources for Wiki users.&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Command_Line_Basics&amp;diff=230</id>
		<title>Command Line Basics</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Command_Line_Basics&amp;diff=230"/>
		<updated>2025-07-11T14:30:11Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page serves as an introduction to basic commands for managing your file system on Linux. Much of what is reviewed on this page is introduced as part of the computing course in Immigration to Statistics. &lt;br /&gt;
&lt;br /&gt;
These commands can be used on any operating system using Linux. To get started, navigate to your terminal, or log in to any of the department servers.&lt;br /&gt;
&lt;br /&gt;
=== Creating a Directory ===&lt;br /&gt;
To create a new directory, you can use the &amp;lt;code&amp;gt;mkdir&amp;lt;/code&amp;gt; command, which quite literally stands for &amp;quot;make directory.&amp;quot; Simply type the command followed by the name of the directory you wish to create:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
mkdir MyDirectory&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Moving Between Directories ===&lt;br /&gt;
The change directory command is called &amp;lt;code&amp;gt;cd&amp;lt;/code&amp;gt; and can be used to efficiently switch between directories. To move into our newly created directory, type:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cd MyDirectory&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;If we want to go backwards, we can use &amp;lt;code&amp;gt;..&amp;lt;/code&amp;gt;, which always means to go back one folder from where you currently are.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cd ..&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;If we want to return to our root directory, we can simply type the command with nothing after it.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;I can check my location using the present working directory command or &amp;lt;code&amp;gt;pwd&amp;lt;/code&amp;gt;. Typing &amp;lt;code&amp;gt;pwd&amp;lt;/code&amp;gt; into the terminal will return the path to your current working directory (i.e. where you are currently located in your file system) from the root.&lt;br /&gt;
&lt;br /&gt;
=== Moving and Copying Files ===&lt;br /&gt;
Say I have a file in MyDirectory called myfile. I might want to move myfile into MySubDirectory. To do this, I would do as follows:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
mv myfile MySubDirectory/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;The first thing after &amp;lt;code&amp;gt;mv&amp;lt;/code&amp;gt; represents the file to move, and the second represents the destination. &amp;lt;code&amp;gt;mv&amp;lt;/code&amp;gt; is also good for renaming files. I can change the name of myfile to mynewname as follows:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
mv myfile mynewname&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This just renames the file in place without moving it elsewhere. &lt;br /&gt;
&lt;br /&gt;
Sometimes, we don&#039;t want to move files but we do want to copy them. This is the &amp;lt;code&amp;gt;cp&amp;lt;/code&amp;gt; command, and it works much the same way as the &amp;lt;code&amp;gt;mv&amp;lt;/code&amp;gt; command. To use &amp;lt;code&amp;gt;cp&amp;lt;/code&amp;gt;, you must specify the file or folder you wish to copy, and the destination to where you want to copy it.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cp myfile ../&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This creates a new copy of myfile in the previous directory on the path to my current working directory. If I have a directory, I might want to copy its entire contents to a new directory. This requires the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag, meaning recursive. This essentially means that for every element in the directory, we recursively copy those files to the new location. Flags can often be added to a command to provide additional information or for greater ease of use. &amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cp -r MyDirectory ../&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This command copies everything that exists in MyDirectory recursively and moves it back one directory from the current working directory.&lt;br /&gt;
&lt;br /&gt;
=== Deleting Files ===&lt;br /&gt;
You must be extremely careful when deleting files through terminal. Unlike when you are using your general user interface and click and drag something to the trash, files cannot easily be restored. Never remove something without being absolutely certain it is what you intend to do and that there are no unintended consequences of its deletion. &lt;br /&gt;
&lt;br /&gt;
The removal command is &amp;lt;code&amp;gt;rm&amp;lt;/code&amp;gt;. Just like with &amp;lt;code&amp;gt;cp&amp;lt;/code&amp;gt;, this can include the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag or not to be done recursively. &amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
rm myfile&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This code simply deletes the file myfile.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
rm -r MyDirectory&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This deletes everything that is in the directory MyDirectory recursively.&lt;br /&gt;
&lt;br /&gt;
=== Determining What is in a Directory ===&lt;br /&gt;
The list command lists all of the files that are in your current working directory. &amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
ls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;You can get more information about different files by using different flags. For instance, the flag &amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt; gives additional information about your read/write access for a file, when it was last created/manipulated, who has access to it, the owner, etc. &amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
ls -l&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;Running just &amp;lt;code&amp;gt;ls&amp;lt;/code&amp;gt; tells you what is in your current working directory, but you can provide a path to determine what is in another directory. The following lists everything that is in MyDirectory, where ~ just indicates the root:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
ls ~/MyDirectory/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Creating and Editing a New File ===&lt;br /&gt;
You can create and edit new files directly in the terminal. There are many commands for doing this: this author&#039;s personal favorite is &amp;lt;code&amp;gt;vim&amp;lt;/code&amp;gt;. &amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
vim my_python_script.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This creates a file called my_python_script.py. When you run this line in the terminal, an editor will appear. To insert information into the editor, click &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;, then type whatever Python code you would like to execute. To stop editing the file, click the escape key and then type &amp;lt;code&amp;gt;:wq!&amp;lt;/code&amp;gt; which will write your files you the file my_python_script.py. You delete entire lines by typing &amp;lt;code&amp;gt;dd&amp;lt;/code&amp;gt;. These are just some of the many useful tools that &amp;lt;code&amp;gt;vim&amp;lt;/code&amp;gt; offers. To edit a file once it has already been created, you need only type &amp;lt;code&amp;gt;vim my_python_script.py&amp;lt;/code&amp;gt; once again.&lt;br /&gt;
&lt;br /&gt;
You can view the contents of a file without using &amp;lt;code&amp;gt;vim&amp;lt;/code&amp;gt; via &amp;lt;code&amp;gt;cat&amp;lt;/code&amp;gt;.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cat my_python_file.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This line of code will show you everything that is in my_python_file.py without taking you to a terminal editor. This is great for viewing logs and output files from running chunks of code on the servers. &lt;br /&gt;
&lt;br /&gt;
If your terminal becomes bogged down with too much text, simply type &amp;lt;code&amp;gt;clear&amp;lt;/code&amp;gt; to reset the view.&lt;br /&gt;
&lt;br /&gt;
=== Running a Python Script ===&lt;br /&gt;
This next part depends on if you have Python installed on your computer and what you have it installed as. Some might have it saved just as &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt;, others might have &amp;lt;code&amp;gt;python3/python2&amp;lt;/code&amp;gt;, depending on if you have multiple versions.&lt;br /&gt;
&lt;br /&gt;
Say I have the following Python code in my_python_file.py, saved in my current working directory:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;Hello, World!&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;I can then do the following on the terminal:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
python my_python_file.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;The terminal will then show &amp;quot;Hello, World!&amp;quot; &lt;br /&gt;
&lt;br /&gt;
I can also redirect the output of the script to another file if I wish to save it. This can be done via:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
python my_python_file.py &amp;gt; output.out&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This saves whatever is printed to a file called output.out. If I ran &amp;lt;code&amp;gt;cat output.out&amp;lt;/code&amp;gt; I would see &amp;quot;Hello, World!&amp;quot; printed.&lt;br /&gt;
&lt;br /&gt;
If I want to run a Python script that may take some time, and I wish to walk away from my computer or do something else in the terminal while it is running, I can use the &amp;lt;code&amp;gt;nohup&amp;lt;/code&amp;gt; command. This will allow the script to run in the background, while I do other things on my computer.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
nohup python my_python_file.py &amp;amp;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;Adding the &amp;amp; allows me to continue working in my current terminal without waiting for the code to finish running.&lt;br /&gt;
&lt;br /&gt;
=== Additional Resources ===&lt;br /&gt;
&lt;br /&gt;
* [https://swcarpentry.github.io/shell-novice/ Software Carpentry]&lt;br /&gt;
* [https://www.learnshell.org/ Learn Shell]&lt;br /&gt;
* [https://mywiki.wooledge.org/BashGuide Bash Guide]&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Main_Page&amp;diff=229</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Main_Page&amp;diff=229"/>
		<updated>2025-07-11T14:25:53Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: /* Computing Office Hours */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Anyone can read this wiki. &amp;amp;nbsp;If you want to write/edit stuff in the wiki, you will need to log in with your account provided by the Statistics &amp;amp; Data Science Department computing group. This Wiki is intended to be a repository for instruction, information and ideas related to the many computing environments available to members and affiliates of the Statistics &amp;amp; Data Science Department.&lt;br /&gt;
&lt;br /&gt;
== Getting to know the computing resources &amp;amp; getting support ==&lt;br /&gt;
&lt;br /&gt;
If you&#039;re interested in knowing about what resources you have access to (and/or) what resources you have access to that might solve a problem you&#039;re having, please visit our [[What_resources_when|&amp;quot;what resources when&amp;quot; article]].&lt;br /&gt;
&lt;br /&gt;
To get support that is beyond this website please attend office hours below and/or email [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu] for support (for more details about how the support ticketing works through [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu] + ServiceNow please see the [[Service_now|ServiceNow]] article).&lt;br /&gt;
&lt;br /&gt;
Reset your StatDS password here - https://util.stat.cmu.edu/passwordreset/&lt;br /&gt;
&lt;br /&gt;
== Department Contacts ==&lt;br /&gt;
&lt;br /&gt;
• [mailto:events@stat.cmu.edu events@stat.cmu.edu]: seminars, conferences &amp;amp; workshops, and calendar updates&lt;br /&gt;
&lt;br /&gt;
• [mailto:news@stat.cmu.edu news@stat.cmu.edu]: dept/dept member-related public news &amp;amp; announcements&lt;br /&gt;
&lt;br /&gt;
• [mailto:lists@stat.cmu.edu lists@stat.cmu.edu]: department email lists&lt;br /&gt;
&lt;br /&gt;
• [mailto:webmaster@stat.cmu.edu webmaster@stat.cmu.edu]: website updates&lt;br /&gt;
&lt;br /&gt;
• [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu]: computer/tech requests&lt;br /&gt;
&lt;br /&gt;
• [mailto:rooms@stat.cmu.edu rooms@stat.cmu.edu]: request space for meetings and TA office hours&lt;br /&gt;
&lt;br /&gt;
• [mailto:reimbursements@stat.cmu.edu reimbursements@stat.cmu.edu]: travel and reimbursement requests&lt;br /&gt;
&lt;br /&gt;
== Computing Office Hours ==&lt;br /&gt;
&lt;br /&gt;
These office hour opportunities provide a dedicated time for you to come ask questions about research computing.&amp;amp;nbsp; These include code advice, how to access certain resources, what the best resources are for your project, or to learn about new connections to help you progress in your education and research. Staff Office hour times will be sent out each week on Monday as a calendar invite; accept any of the invitations to add it to your personal calendar. PhD TA Office Hours are below, unless a different time is&amp;amp;nbsp;communicated.&lt;br /&gt;
&lt;br /&gt;
#PhD TA Office Hours for the first half of summer 2025 are held by Elizabeth Cucuzzella in FMS Room 320 on Mondays from 2:30 pm to 4:30 pm. PhD students can also message on the #computing channel on slack.&lt;br /&gt;
#Staff Office Hours are held by appointment by Carl Skipper in BH 129G.  Schedule via [mailto:lists@stat.cmu.edu email].&lt;br /&gt;
&lt;br /&gt;
See [[Computing_office_hour_usage|computing office hour usage]] for what office hours can be used for.&lt;br /&gt;
&lt;br /&gt;
== Computing Wiki Pages ==&lt;br /&gt;
&lt;br /&gt;
=== Managing servers and environments ===&lt;br /&gt;
&lt;br /&gt;
==== Virtual environments ====&lt;br /&gt;
*[[Virtual Environment Introduction|Why use virtual environments?]]&lt;br /&gt;
*R&#039;s [[Renv]]&lt;br /&gt;
*Python&#039;s [[Python_virtual_environments|virtual environments (2 options)]]&lt;br /&gt;
*Old instructions for [[Conda with Python (Reference Only)|conda with Python]]&lt;br /&gt;
&lt;br /&gt;
==== Containers ====&lt;br /&gt;
*[[Introduction to containers]]&lt;br /&gt;
*[[Docker, and why-you-should-use-it-for-your-research]]&lt;br /&gt;
*[[Tutorials|Basic tutorial]]&lt;br /&gt;
*[[Running Docker locally and on Hydras]]&lt;br /&gt;
&lt;br /&gt;
=== Department computing resources ===&lt;br /&gt;
&lt;br /&gt;
==== Printing ====&lt;br /&gt;
*[[Department_Printers|Department Printers]] &lt;br /&gt;
&lt;br /&gt;
==== Web Hosting ====&lt;br /&gt;
#[[How to host your webpage under stat.cmu.edu]]&lt;br /&gt;
#[[Bulding your website with Hugo]]&lt;br /&gt;
&lt;br /&gt;
==== Servers ====&lt;br /&gt;
*[[Linux_and_department_servers|Department Servers]]. This wiki introduces you to the &#039;&#039;&#039;hydra&#039;&#039;&#039; servers, basic Linux commands, and more. &lt;br /&gt;
*#[[Terminal_multiplexers|Terminal multiplexers (screen and tmux)]]. This wiki presents tools for use on the &#039;&#039;hydra&#039;&#039; that can allow you to run jobs even when your not on the server, and much much more!&lt;br /&gt;
*#[[Python on the server]]. This wiki introduces uses command line approaches to running &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt; on a virtual machine, and how to use jupyter notebooks through the server as well.&lt;br /&gt;
*#[[R on the server]]. This wiki introduces uses command line approaches to running &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; on a virtual machine&lt;br /&gt;
&lt;br /&gt;
*[[Department GPU Servers]], for small jobs needing 1 GPU.&lt;br /&gt;
*[[GPU Cluster]], for larger jobs that may need multiple GPUs and many CPUs.&lt;br /&gt;
*[[PSC]], the Pittsburgh Supercomputing Center. Free accounts and usage credits are available &lt;br /&gt;
*#[[ACCESS]]&lt;br /&gt;
*#[[Slurm]]. This wiki introduces the job management system uses at PSC.&lt;br /&gt;
*#[[Parallelization_on_PSC/Bridges-2]]&lt;br /&gt;
*#[[Access PSC compute nodes directly from VSCode]]&lt;br /&gt;
*Azure&lt;br /&gt;
*#[[Azure_getting_started|Azure: getting started]]. This wiki walks you through the steps to create a virtual machine through Azure&lt;br /&gt;
*#[[How to obtain a virtual machine with a GPU]]&lt;br /&gt;
*#[[Attach a disk to a virtual machine]]&lt;br /&gt;
&lt;br /&gt;
=== Development guides ===&lt;br /&gt;
&lt;br /&gt;
====[[Parallel_computing|Parallelization]]====&lt;br /&gt;
*[[Parallelization_in_r|R-specific parallelization]]&lt;br /&gt;
*[[Parallelization_in_python|Python-specific parallelization]]&lt;br /&gt;
&lt;br /&gt;
==== Package development ====&lt;br /&gt;
*[[Developing_R_packages|R package development]]&lt;br /&gt;
*[[Developing_python_packages|Python package development]]&lt;br /&gt;
[[Terminal automation]]. This wiki introduces approaches to automate Linux/OS command line calls.&lt;br /&gt;
*[[Bash files]]. This wiki presents potential uses of Bash files including automated file/folder creation, parallelization and much more!&lt;br /&gt;
*[[Make]]. This wiki presents potential uses of &amp;lt;code&amp;gt;make&amp;lt;/code&amp;gt; and Makefiles including automated workflow and dependency tracking&lt;br /&gt;
*[[Jumping to frequently used folders and automating environment setups]]&lt;br /&gt;
&lt;br /&gt;
==== Version Control ====&lt;br /&gt;
See the guide provided in the site of [https://36-750.github.io/tools/version-control/ 36750 - Statistical Computing]&lt;br /&gt;
&lt;br /&gt;
==== Reference management ====&lt;br /&gt;
#Zotero &lt;br /&gt;
#Mendeley &lt;br /&gt;
&lt;br /&gt;
==== Integrated Development Environments (IDEs) ====&lt;br /&gt;
#[[VSCode|VSCode]] &lt;br /&gt;
#PyCharm &lt;br /&gt;
#RStudio &lt;br /&gt;
#Eclipse &lt;br /&gt;
&lt;br /&gt;
==== Text Editors ====&lt;br /&gt;
#Vim &lt;br /&gt;
#Nano &lt;br /&gt;
#Emacs &lt;br /&gt;
#Atom &lt;br /&gt;
#Sublime &lt;br /&gt;
&lt;br /&gt;
See also the guide provided in the site of [https://36-750.github.io/tools/editors/ 36750 - Statistical Computing]&lt;br /&gt;
&lt;br /&gt;
==== AI Tools ====&lt;br /&gt;
* [[ChatGPT and other LLMs]]&lt;br /&gt;
&lt;br /&gt;
*[[github_copilot|GitHub Copilot]]&lt;br /&gt;
&lt;br /&gt;
==== Miscellaneous====&lt;br /&gt;
#[[Windows_Subsystem_for_Linux_(WSL)_and_Python|Windows&amp;amp;nbsp;Subsystem for Linux (WSL) and Python]]&lt;br /&gt;
#[[Amphetamine|Amphetamine: How to keep your computer awake]]&lt;br /&gt;
&lt;br /&gt;
===General Department Computing Resources===&lt;br /&gt;
&lt;br /&gt;
*Fall 2021 [[Computing_seminar_resources|Computing Seminar Resources]] (video and slides)&lt;br /&gt;
*[[Basic Linux Commands|Introduction to Basic Linux Commands]]&lt;br /&gt;
&lt;br /&gt;
==Ideas for the Wiki==&lt;br /&gt;
&lt;br /&gt;
Please see the [[TODO|TODO page]] to post suggestions and errata.&lt;br /&gt;
&lt;br /&gt;
==Using the Wiki==&lt;br /&gt;
&lt;br /&gt;
*Here is some&amp;amp;nbsp;[[Generic_Wiki_Information|Generic Wiki Information]]&amp;amp;nbsp;that you may be interested in, such as a users&#039; guide and other resources for Wiki users.&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Command_Line_Basics&amp;diff=228</id>
		<title>Command Line Basics</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Command_Line_Basics&amp;diff=228"/>
		<updated>2025-07-10T19:44:44Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page serves as an introduction to basic commands for managing your file system on Linux. Much of what is reviewed on this page is introduced as part of the computing course in Immigration to Statistics. &lt;br /&gt;
&lt;br /&gt;
These commands can be used on any operating system using Linux. To get started, navigate to your terminal, or log in to any of the department servers.&lt;br /&gt;
&lt;br /&gt;
=== Creating a Directory ===&lt;br /&gt;
To create a new directory, you can use the &amp;lt;code&amp;gt;mkdir&amp;lt;/code&amp;gt; command, which quite literally stands for &amp;quot;make directory.&amp;quot; Simply type the command followed by the name of the directory you wish to create:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
mkdir MyDirectory&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Moving Between Directories ===&lt;br /&gt;
The change directory command is called &amp;lt;code&amp;gt;cd&amp;lt;/code&amp;gt; and can be used to efficiently switch between directories. To move into our newly created directory, type:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cd MyDirectory&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;If we want to go backwards, we can use &amp;lt;code&amp;gt;..&amp;lt;/code&amp;gt;, which always means to go back one folder from where you currently are.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cd ..&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;If we want to return to our root directory, we can simply type the command with nothing after it.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;I can check my location using the present working directory command or &amp;lt;code&amp;gt;pwd&amp;lt;/code&amp;gt;. Typing &amp;lt;code&amp;gt;pwd&amp;lt;/code&amp;gt; into the terminal will return the path to your current working directory (i.e. where you are currently located in your file system) from the root.&lt;br /&gt;
&lt;br /&gt;
=== Moving and Copying Files ===&lt;br /&gt;
Say I have a file in MyDirectory called myfile. I might want to move myfile into MySubDirectory. To do this, I would do as follows:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
mv myfile MySubDirectory/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;The first thing after &amp;lt;code&amp;gt;mv&amp;lt;/code&amp;gt; represents the file to move, and the second represents the destination. &amp;lt;code&amp;gt;mv&amp;lt;/code&amp;gt; is also good for renaming files. I can change the name of myfile to mynewname as follows:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
mv myfile mynewname&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This just renames the file in place without moving it elsewhere. &lt;br /&gt;
&lt;br /&gt;
Sometimes, we don&#039;t want to move files but we do want to copy them. This is the &amp;lt;code&amp;gt;cp&amp;lt;/code&amp;gt; command, and it works much the same way as the &amp;lt;code&amp;gt;mv&amp;lt;/code&amp;gt; command. To use &amp;lt;code&amp;gt;cp&amp;lt;/code&amp;gt;, you must specify the file or folder you wish to copy, and the destination to where you want to copy it.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cp myfile ../&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This creates a new copy of myfile in the previous directory on the path to my current working directory. If I have a directory, I might want to copy its entire contents to a new directory. This requires the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag, meaning recursive. This essentially means that for every element in the directory, we recursively copy those files to the new location. Flags can often be added to a command to provide additional information or for greater ease of use. &amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cp -r MyDirectory ../&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This command copies everything that exists in MyDirectory recursively and moves it back one directory from the current working directory.&lt;br /&gt;
&lt;br /&gt;
=== Deleting Files ===&lt;br /&gt;
You must be extremely careful when deleting files through terminal. Unlike when you are using your general user interface and click and drag something to the trash, files cannot easily be restored. Never remove something without being absolutely certain it is what you intend to do and that there are no unintended consequences of its deletion. &lt;br /&gt;
&lt;br /&gt;
The removal command is &amp;lt;code&amp;gt;rm&amp;lt;/code&amp;gt;. Just like with &amp;lt;code&amp;gt;cp&amp;lt;/code&amp;gt;, this can include the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag or not to be done recursively. &amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
rm myfile&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This code simply deletes the file myfile.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
rm -r MyDirectory&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This deletes everything that is in the directory MyDirectory recursively.&lt;br /&gt;
&lt;br /&gt;
=== Determining What is in a Directory ===&lt;br /&gt;
The list command lists all of the files that are in your current working directory. &amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
ls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;You can get more information about different files by using different flags. For instance, the flag &amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt; gives additional information about your read/write access for a file, when it was last created/manipulated, who has access to it, the owner, etc. &amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
ls -l&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;Running just &amp;lt;code&amp;gt;ls&amp;lt;/code&amp;gt; tells you what is in your current working directory, but you can provide a path to determine what is in another directory. The following lists everything that is in MyDirectory, where ~ just indicates the root:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
ls ~/MyDirectory/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Creating and Editing a New File ===&lt;br /&gt;
You can create and edit new files directly in the terminal. There are many commands for doing this: this author&#039;s personal favorite is &amp;lt;code&amp;gt;vim&amp;lt;/code&amp;gt;. &amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
vim my_python_script.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This creates a file called my_python_script.py. When you run this line in the terminal, an editor will appear. To insert information into the editor, click &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;, then type whatever Python code you would like to execute. To stop editing the file, click the escape key and then type &amp;lt;code&amp;gt;:wq!&amp;lt;/code&amp;gt; which will write your files you the file my_python_script.py. You delete entire lines by typing &amp;lt;code&amp;gt;dd&amp;lt;/code&amp;gt;. These are just some of the many useful tools that &amp;lt;code&amp;gt;vim&amp;lt;/code&amp;gt; offers. To edit a file once it has already been created, you need only type &amp;lt;code&amp;gt;vim my_python_script.py&amp;lt;/code&amp;gt; once again.&lt;br /&gt;
&lt;br /&gt;
You can view the contents of a file without using &amp;lt;code&amp;gt;vim&amp;lt;/code&amp;gt; via &amp;lt;code&amp;gt;cat&amp;lt;/code&amp;gt;.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cat my_python_file.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This line of code will show you everything that is in my_python_file.py without taking you to a terminal editor. This is great for viewing logs and output files from running chunks of code on the servers. &lt;br /&gt;
&lt;br /&gt;
If your terminal becomes bogged down with too much text, simply type &amp;lt;code&amp;gt;clear&amp;lt;/code&amp;gt; to reset the view.&lt;br /&gt;
&lt;br /&gt;
=== Running a Python Script ===&lt;br /&gt;
This next part depends on if you have Python installed on your computer and what you have it installed as. Some might have it saved just as &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt;, others might have &amp;lt;code&amp;gt;python3/python2&amp;lt;/code&amp;gt;, depending on if you have multiple versions.&lt;br /&gt;
&lt;br /&gt;
Say I have the following Python code in my_python_file.py, saved in my current working directory:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print(&amp;quot;Hello, World!&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;I can then do the following on the terminal:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
python my_python_file.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;The terminal will then show &amp;quot;Hello, World!&amp;quot; &lt;br /&gt;
&lt;br /&gt;
I can also redirect the output of the script to another file if I wish to save it. This can be done via:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
python my_python_file.py &amp;gt; output.out&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This saves whatever is printed to a file called output.out. If I ran &amp;lt;code&amp;gt;cat output.out&amp;lt;/code&amp;gt; I would see &amp;quot;Hello, World!&amp;quot; printed.&lt;br /&gt;
&lt;br /&gt;
If I want to run a Python script that may take some time, and I wish to walk away from my computer or do something else in the terminal while it is running, I can use the &amp;lt;code&amp;gt;nohup&amp;lt;/code&amp;gt; command. This will allow the script to run in the background, while I do other things on my computer.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
nohup python my_python_file.py &amp;amp;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;Adding the &amp;amp; allows me to continue working in my current terminal without waiting for the code to finish running.&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Command_Line_Basics&amp;diff=227</id>
		<title>Command Line Basics</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Command_Line_Basics&amp;diff=227"/>
		<updated>2025-07-10T19:33:40Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page serves as an introduction to basic commands for managing your file system on Linux. Much of what is reviewed on this page is introduced as part of the computing course in Immigration to Statistics. &lt;br /&gt;
&lt;br /&gt;
These commands can be used on any operating system using Linux. To get started, navigate to your terminal, or log in to any of the department servers.&lt;br /&gt;
&lt;br /&gt;
=== Creating a Directory ===&lt;br /&gt;
To create a new directory, you can use the &amp;lt;code&amp;gt;mkdir&amp;lt;/code&amp;gt; command, which quite literally stands for &amp;quot;make directory.&amp;quot; Simply type the command followed by the name of the directory you wish to create:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
mkdir MyDirectory&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Moving Between Directories ===&lt;br /&gt;
The change directory command is called &amp;lt;code&amp;gt;cd&amp;lt;/code&amp;gt; and can be used to efficiently switch between directories. To move into our newly created directory, type:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cd MyDirectory&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;If we want to go backwards, we can use &amp;lt;code&amp;gt;..&amp;lt;/code&amp;gt;, which always means to go back one folder from where you currently are.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cd ..&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;If we want to return to our root directory, we can simply type the command with nothing after it.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;I can check my location using the present working directory command or &amp;lt;code&amp;gt;pwd&amp;lt;/code&amp;gt;. Typing &amp;lt;code&amp;gt;pwd&amp;lt;/code&amp;gt; into the terminal will return the path to your current working directory (i.e. where you are currently located in your file system) from the root.&lt;br /&gt;
&lt;br /&gt;
=== Moving and Copying Files ===&lt;br /&gt;
Say I have a file in MyDirectory called myfile. I might want to move myfile into MySubDirectory. To do this, I would do as follows:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
mv myfile MySubDirectory/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;The first thing after &amp;lt;code&amp;gt;mv&amp;lt;/code&amp;gt; represents the file to move, and the second represents the destination. &amp;lt;code&amp;gt;mv&amp;lt;/code&amp;gt; is also good for renaming files. I can change the name of myfile to mynewname as follows:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
mv myfile mynewname&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This just renames the file in place without moving it elsewhere. &lt;br /&gt;
&lt;br /&gt;
Sometimes, we don&#039;t want to move files but we do want to copy them. This is the &amp;lt;code&amp;gt;cp&amp;lt;/code&amp;gt; command, and it works much the same way as the &amp;lt;code&amp;gt;mv&amp;lt;/code&amp;gt; command. To use &amp;lt;code&amp;gt;cp&amp;lt;/code&amp;gt;, you must specify the file or folder you wish to copy, and the destination to where you want to copy it.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cp myfile ../&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This creates a new copy of myfile in the previous directory on the path to my current working directory. If I have a directory, I might want to copy its entire contents to a new directory. This requires the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag, meaning recursive. This essentially means that for every element in the directory, we recursively copy those files to the new location. Flags can often be added to a command to provide additional information or for greater ease of use. &amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cp -r MyDirectory ../&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This command copies everything that exists in MyDirectory recursively and moves it back one directory from the current working directory.&lt;br /&gt;
&lt;br /&gt;
=== Deleting Files ===&lt;br /&gt;
You must be extremely careful when deleting files through terminal. Unlike when you are using your general user interface and click and drag something to the trash, files cannot easily be restored. Never remove something without being absolutely certain it is what you intend to do and that there are no unintended consequences of its deletion. &lt;br /&gt;
&lt;br /&gt;
The removal command is &amp;lt;code&amp;gt;rm&amp;lt;/code&amp;gt;. Just like with &amp;lt;code&amp;gt;cp&amp;lt;/code&amp;gt;, this can include the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag or not to be done recursively. &amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
rm myfile&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This code simply deletes the file myfile.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
rm -r MyDirectory&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This deletes everything that is in the directory MyDirectory recursively.&lt;br /&gt;
&lt;br /&gt;
=== Determining What is in a Directory ===&lt;br /&gt;
The list command lists all of the files that are in your current working directory. &amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
ls&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;You can get more information about different files by using different flags. For instance, the flag &amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt; gives additional information about your read/write access for a file, when it was last created/manipulated, who has access to it, the owner, etc. &amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
ls -l&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;Running just &amp;lt;code&amp;gt;ls&amp;lt;/code&amp;gt; tells you what is in your current working directory, but you can provide a path to determine what is in another directory. The following lists everything that is in MyDirectory, where ~ just indicates the root:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
ls ~/MyDirectory/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Main_Page&amp;diff=226</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Main_Page&amp;diff=226"/>
		<updated>2025-07-10T19:23:06Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: /* General Department Computing Resources */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Anyone can read this wiki. &amp;amp;nbsp;If you want to write/edit stuff in the wiki, you will need to log in with your account provided by the Statistics &amp;amp; Data Science Department computing group. This Wiki is intended to be a repository for instruction, information and ideas related to the many computing environments available to members and affiliates of the Statistics &amp;amp; Data Science Department.&lt;br /&gt;
&lt;br /&gt;
== Getting to know the computing resources &amp;amp; getting support ==&lt;br /&gt;
&lt;br /&gt;
If you&#039;re interested in knowing about what resources you have access to (and/or) what resources you have access to that might solve a problem you&#039;re having, please visit our [[What_resources_when|&amp;quot;what resources when&amp;quot; article]].&lt;br /&gt;
&lt;br /&gt;
To get support that is beyond this website please attend office hours below and/or email [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu] for support (for more details about how the support ticketing works through [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu] + ServiceNow please see the [[Service_now|ServiceNow]] article).&lt;br /&gt;
&lt;br /&gt;
Reset your StatDS password here - https://util.stat.cmu.edu/passwordreset/&lt;br /&gt;
&lt;br /&gt;
== Department Contacts ==&lt;br /&gt;
&lt;br /&gt;
• [mailto:events@stat.cmu.edu events@stat.cmu.edu]: seminars, conferences &amp;amp; workshops, and calendar updates&lt;br /&gt;
&lt;br /&gt;
• [mailto:news@stat.cmu.edu news@stat.cmu.edu]: dept/dept member-related public news &amp;amp; announcements&lt;br /&gt;
&lt;br /&gt;
• [mailto:lists@stat.cmu.edu lists@stat.cmu.edu]: department email lists&lt;br /&gt;
&lt;br /&gt;
• [mailto:webmaster@stat.cmu.edu webmaster@stat.cmu.edu]: website updates&lt;br /&gt;
&lt;br /&gt;
• [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu]: computer/tech requests&lt;br /&gt;
&lt;br /&gt;
• [mailto:rooms@stat.cmu.edu rooms@stat.cmu.edu]: request space for meetings and TA office hours&lt;br /&gt;
&lt;br /&gt;
• [mailto:reimbursements@stat.cmu.edu reimbursements@stat.cmu.edu]: travel and reimbursement requests&lt;br /&gt;
&lt;br /&gt;
== Computing Office Hours ==&lt;br /&gt;
&lt;br /&gt;
These office hour opportunities provide a dedicated time for you to come ask questions about research computing.&amp;amp;nbsp; These include code advice, how to access certain resources, what the best resources are for your project, or to learn about new connections to help you progress in your education and research. Staff Office hour times will be sent out each week on Monday as a calendar invite; accept any of the invitations to add it to your personal calendar. PhD TA Office Hours are below, unless a different time is&amp;amp;nbsp;communicated.&lt;br /&gt;
&lt;br /&gt;
#PhD TA Office Hours for the first half of summer 2025 are held by Julia Walchessen in FMS Room 320 on Mondays from 2:30 pm to 4:30 pm&lt;br /&gt;
#Staff Office Hours are held by appointment  by Carl Skipper in BH 129G.  Schedule via [mailto:lists@stat.cmu.edu email].&lt;br /&gt;
&lt;br /&gt;
See [[Computing_office_hour_usage|computing office hour usage]] for what office hours can be used for.&lt;br /&gt;
&lt;br /&gt;
== Computing Wiki Pages ==&lt;br /&gt;
&lt;br /&gt;
=== Managing servers and environments ===&lt;br /&gt;
&lt;br /&gt;
==== Virtual environments ====&lt;br /&gt;
*[[Virtual Environment Introduction|Why use virtual environments?]]&lt;br /&gt;
*R&#039;s [[Renv]]&lt;br /&gt;
*Python&#039;s [[Python_virtual_environments|virtual environments (2 options)]]&lt;br /&gt;
*Old instructions for [[Conda with Python (Reference Only)|conda with Python]]&lt;br /&gt;
&lt;br /&gt;
==== Containers ====&lt;br /&gt;
*[[Introduction to containers]]&lt;br /&gt;
*[[Docker, and why-you-should-use-it-for-your-research]]&lt;br /&gt;
*[[Tutorials|Basic tutorial]]&lt;br /&gt;
*[[Running Docker locally and on Hydras]]&lt;br /&gt;
&lt;br /&gt;
=== Department computing resources ===&lt;br /&gt;
&lt;br /&gt;
==== Printing ====&lt;br /&gt;
*[[Department_Printers|Department Printers]] &lt;br /&gt;
&lt;br /&gt;
==== Web Hosting ====&lt;br /&gt;
#[[How to host your webpage under stat.cmu.edu]]&lt;br /&gt;
#[[Bulding your website with Hugo]]&lt;br /&gt;
&lt;br /&gt;
==== Servers ====&lt;br /&gt;
*[[Linux_and_department_servers|Department Servers]]. This wiki introduces you to the &#039;&#039;&#039;hydra&#039;&#039;&#039; servers, basic Linux commands, and more. &lt;br /&gt;
*#[[Terminal_multiplexers|Terminal multiplexers (screen and tmux)]]. This wiki presents tools for use on the &#039;&#039;hydra&#039;&#039; that can allow you to run jobs even when your not on the server, and much much more!&lt;br /&gt;
*#[[Python on the server]]. This wiki introduces uses command line approaches to running &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt; on a virtual machine, and how to use jupyter notebooks through the server as well.&lt;br /&gt;
*#[[R on the server]]. This wiki introduces uses command line approaches to running &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; on a virtual machine&lt;br /&gt;
&lt;br /&gt;
*[[Department GPU Servers]], for small jobs needing 1 GPU.&lt;br /&gt;
*[[GPU Cluster]], for larger jobs that may need multiple GPUs and many CPUs.&lt;br /&gt;
*[[PSC]], the Pittsburgh Supercomputing Center. Free accounts and usage credits are available &lt;br /&gt;
*#[[ACCESS]]&lt;br /&gt;
*#[[Slurm]]. This wiki introduces the job management system uses at PSC.&lt;br /&gt;
*#[[Parallelization_on_PSC/Bridges-2]]&lt;br /&gt;
*#[[Access PSC compute nodes directly from VSCode]]&lt;br /&gt;
*Azure&lt;br /&gt;
*#[[Azure_getting_started|Azure: getting started]]. This wiki walks you through the steps to create a virtual machine through Azure&lt;br /&gt;
*#[[How to obtain a virtual machine with a GPU]]&lt;br /&gt;
*#[[Attach a disk to a virtual machine]]&lt;br /&gt;
&lt;br /&gt;
=== Development guides ===&lt;br /&gt;
&lt;br /&gt;
====[[Parallel_computing|Parallelization]]====&lt;br /&gt;
*[[Parallelization_in_r|R-specific parallelization]]&lt;br /&gt;
*[[Parallelization_in_python|Python-specific parallelization]]&lt;br /&gt;
&lt;br /&gt;
==== Package development ====&lt;br /&gt;
*[[Developing_R_packages|R package development]]&lt;br /&gt;
*[[Developing_python_packages|Python package development]]&lt;br /&gt;
[[Terminal automation]]. This wiki introduces approaches to automate Linux/OS command line calls.&lt;br /&gt;
*[[Bash files]]. This wiki presents potential uses of Bash files including automated file/folder creation, parallelization and much more!&lt;br /&gt;
*[[Make]]. This wiki presents potential uses of &amp;lt;code&amp;gt;make&amp;lt;/code&amp;gt; and Makefiles including automated workflow and dependency tracking&lt;br /&gt;
*[[Jumping to frequently used folders and automating environment setups]]&lt;br /&gt;
&lt;br /&gt;
==== Version Control ====&lt;br /&gt;
See the guide provided in the site of [https://36-750.github.io/tools/version-control/ 36750 - Statistical Computing]&lt;br /&gt;
&lt;br /&gt;
==== Reference management ====&lt;br /&gt;
#Zotero &lt;br /&gt;
#Mendeley &lt;br /&gt;
&lt;br /&gt;
==== Integrated Development Environments (IDEs) ====&lt;br /&gt;
#[[VSCode|VSCode]] &lt;br /&gt;
#PyCharm &lt;br /&gt;
#RStudio &lt;br /&gt;
#Eclipse &lt;br /&gt;
&lt;br /&gt;
==== Text Editors ====&lt;br /&gt;
#Vim &lt;br /&gt;
#Nano &lt;br /&gt;
#Emacs &lt;br /&gt;
#Atom &lt;br /&gt;
#Sublime &lt;br /&gt;
&lt;br /&gt;
See also the guide provided in the site of [https://36-750.github.io/tools/editors/ 36750 - Statistical Computing]&lt;br /&gt;
&lt;br /&gt;
==== AI Tools ====&lt;br /&gt;
* [[ChatGPT and other LLMs]]&lt;br /&gt;
&lt;br /&gt;
*[[github_copilot|GitHub Copilot]]&lt;br /&gt;
&lt;br /&gt;
==== Miscellaneous====&lt;br /&gt;
#[[Windows_Subsystem_for_Linux_(WSL)_and_Python|Windows&amp;amp;nbsp;Subsystem for Linux (WSL) and Python]]&lt;br /&gt;
#[[Amphetamine|Amphetamine: How to keep your computer awake]]&lt;br /&gt;
&lt;br /&gt;
===General Department Computing Resources===&lt;br /&gt;
&lt;br /&gt;
*Fall 2021 [[Computing_seminar_resources|Computing Seminar Resources]] (video and slides)&lt;br /&gt;
*[[Basic Linux Commands|Introduction to Basic Linux Commands]]&lt;br /&gt;
&lt;br /&gt;
==Ideas for the Wiki==&lt;br /&gt;
&lt;br /&gt;
Please see the [[TODO|TODO page]] to post suggestions and errata.&lt;br /&gt;
&lt;br /&gt;
==Using the Wiki==&lt;br /&gt;
&lt;br /&gt;
*Here is some&amp;amp;nbsp;[[Generic_Wiki_Information|Generic Wiki Information]]&amp;amp;nbsp;that you may be interested in, such as a users&#039; guide and other resources for Wiki users.&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Command_Line_Basics&amp;diff=225</id>
		<title>Command Line Basics</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Command_Line_Basics&amp;diff=225"/>
		<updated>2025-07-10T19:22:49Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page serves as an introduction to basic commands for managing your file system on Linux. Much of what is reviewed on this page is introduced as part of the computing course in Immigration to Statistics. &lt;br /&gt;
&lt;br /&gt;
These commands can be used on any operating system using Linux. To get started, navigate to your terminal, or log in to any of the department servers.&lt;br /&gt;
&lt;br /&gt;
=== Creating a Directory ===&lt;br /&gt;
To create a new directory, you can use the &amp;lt;code&amp;gt;mkdir&amp;lt;/code&amp;gt; command, which quite literally stands for &amp;quot;make directory.&amp;quot; Simply type the command followed by the name of the directory you wish to create:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
mkdir MyDirectory&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Moving Between Directories ===&lt;br /&gt;
The change directory command is called &amp;lt;code&amp;gt;cd&amp;lt;/code&amp;gt; and can be used to efficiently switch between directories. To move into our newly created directory, type:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cd MyDirectory&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;If we want to go backwards, we can use &amp;lt;code&amp;gt;..&amp;lt;/code&amp;gt;, which always means to go back one folder from where you currently are.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cd ..&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;If we want to return to our root directory, we can simply type the command with nothing after it.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cd&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;I can check my location using the present working directory command or &amp;lt;code&amp;gt;pwd&amp;lt;/code&amp;gt;. Typing &amp;lt;code&amp;gt;pwd&amp;lt;/code&amp;gt; into the terminal will return the path to your current working directory (i.e. where you are currently located in your file system) from the root.&lt;br /&gt;
&lt;br /&gt;
=== Moving and Copying Files ===&lt;br /&gt;
Say I have a file in MyDirectory called myfile. I might want to move myfile into MySubDirectory. To do this, I would do as follows:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
mv myfile MySubDirectory/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;The first thing after &amp;lt;code&amp;gt;mv&amp;lt;/code&amp;gt; represents the file to move, and the second represents the destination. &amp;lt;code&amp;gt;mv&amp;lt;/code&amp;gt; is also good for renaming files. I can change the name of myfile to mynewname as follows:&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
mv myfile mynewname&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This just renames the file in place without moving it elsewhere. &lt;br /&gt;
&lt;br /&gt;
If I have a directory, I might want to move its entire contents to a new directory. This requires the &amp;lt;code&amp;gt;-r&amp;lt;/code&amp;gt; flag, meaning recursive. This essentially means that for every element in the directory, we recursively move those files to the new location. Flags can often be added to a command to provide additional information or for greater ease of use.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
mv -r MySubDirectory ../MyNewDirectory/&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This code recursively moves everything that is in MySubDirectory into MyNewDirectory, which is located one folder back from our current location in the terminal.&lt;br /&gt;
&lt;br /&gt;
Sometimes, we don&#039;t want to move files but we do want to copy them. This is the &amp;lt;code&amp;gt;cp&amp;lt;/code&amp;gt; command, and it works much the same way as the &amp;lt;code&amp;gt;mv&amp;lt;/code&amp;gt; command. To use &amp;lt;code&amp;gt;cp&amp;lt;/code&amp;gt;, you must specify the file or folder you wish to copy, and the destination to where you want to copy it.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cp myfile ../&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This creates a new copy of myfile in the previous directory on the path to my current working directory. We can also copy over the entire contents of a folder using the recursive flag.&amp;lt;syntaxhighlight lang=&amp;quot;linux-config&amp;quot;&amp;gt;&lt;br /&gt;
cp -r MyDirectory ../&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Deleting Files ===&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Command_Line_Basics&amp;diff=224</id>
		<title>Command Line Basics</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Command_Line_Basics&amp;diff=224"/>
		<updated>2025-07-10T19:04:56Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: creating basic linux commands the right way&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page serves as an introduction to basic commands for managing your file system on Linux.&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Index.php%3Ftitle%3DBasicLinuxCommands&amp;diff=223</id>
		<title>Index.php?title=BasicLinuxCommands</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Index.php%3Ftitle%3DBasicLinuxCommands&amp;diff=223"/>
		<updated>2025-07-10T19:03:03Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: Introduction to Basic Linux Commands&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This is an introduction to basic linux commands page&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Main_Page&amp;diff=222</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Main_Page&amp;diff=222"/>
		<updated>2025-07-10T17:53:24Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: /* Miscellanea */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Anyone can read this wiki. &amp;amp;nbsp;If you want to write/edit stuff in the wiki, you will need to log in with your account provided by the Statistics &amp;amp; Data Science Department computing group. This Wiki is intended to be a repository for instruction, information and ideas related to the many computing environments available to members and affiliates of the Statistics &amp;amp; Data Science Department.&lt;br /&gt;
&lt;br /&gt;
== Getting to know the computing resources &amp;amp; getting support ==&lt;br /&gt;
&lt;br /&gt;
If you&#039;re interested in knowing about what resources you have access to (and/or) what resources you have access to that might solve a problem you&#039;re having, please visit our [[What_resources_when|&amp;quot;what resources when&amp;quot; article]].&lt;br /&gt;
&lt;br /&gt;
To get support that is beyond this website please attend office hours below and/or email [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu] for support (for more details about how the support ticketing works through [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu] + ServiceNow please see the [[Service_now|ServiceNow]] article).&lt;br /&gt;
&lt;br /&gt;
Reset your StatDS password here - https://util.stat.cmu.edu/passwordreset/&lt;br /&gt;
&lt;br /&gt;
== Department Contacts ==&lt;br /&gt;
&lt;br /&gt;
• [mailto:events@stat.cmu.edu events@stat.cmu.edu]: seminars, conferences &amp;amp; workshops, and calendar updates&lt;br /&gt;
&lt;br /&gt;
• [mailto:news@stat.cmu.edu news@stat.cmu.edu]: dept/dept member-related public news &amp;amp; announcements&lt;br /&gt;
&lt;br /&gt;
• [mailto:lists@stat.cmu.edu lists@stat.cmu.edu]: department email lists&lt;br /&gt;
&lt;br /&gt;
• [mailto:webmaster@stat.cmu.edu webmaster@stat.cmu.edu]: website updates&lt;br /&gt;
&lt;br /&gt;
• [mailto:remarks@stat.cmu.edu remarks@stat.cmu.edu]: computer/tech requests&lt;br /&gt;
&lt;br /&gt;
• [mailto:rooms@stat.cmu.edu rooms@stat.cmu.edu]: request space for meetings and TA office hours&lt;br /&gt;
&lt;br /&gt;
• [mailto:reimbursements@stat.cmu.edu reimbursements@stat.cmu.edu]: travel and reimbursement requests&lt;br /&gt;
&lt;br /&gt;
== Computing Office Hours ==&lt;br /&gt;
&lt;br /&gt;
These office hour opportunities provide a dedicated time for you to come ask questions about research computing.&amp;amp;nbsp; These include code advice, how to access certain resources, what the best resources are for your project, or to learn about new connections to help you progress in your education and research. Staff Office hour times will be sent out each week on Monday as a calendar invite; accept any of the invitations to add it to your personal calendar. PhD TA Office Hours are below, unless a different time is&amp;amp;nbsp;communicated.&lt;br /&gt;
&lt;br /&gt;
#PhD TA Office Hours for the first half of summer 2025 are held by Julia Walchessen in FMS Room 320 on Mondays from 2:30 pm to 4:30 pm&lt;br /&gt;
#Staff Office Hours are held by appointment  by Carl Skipper in BH 129G.  Schedule via [mailto:lists@stat.cmu.edu email].&lt;br /&gt;
&lt;br /&gt;
See [[Computing_office_hour_usage|computing office hour usage]] for what office hours can be used for.&lt;br /&gt;
&lt;br /&gt;
== Computing Wiki Pages ==&lt;br /&gt;
&lt;br /&gt;
=== Managing servers and environments ===&lt;br /&gt;
&lt;br /&gt;
==== Virtual environments ====&lt;br /&gt;
*[[Virtual Environment Introduction|Why use virtual environments?]]&lt;br /&gt;
*R&#039;s [[Renv]]&lt;br /&gt;
*Python&#039;s [[Python_virtual_environments|virtual environments (2 options)]]&lt;br /&gt;
*Old instructions for [[Conda with Python (Reference Only)|conda with Python]]&lt;br /&gt;
&lt;br /&gt;
==== Containers ====&lt;br /&gt;
*[[Introduction to containers]]&lt;br /&gt;
*[[Docker, and why-you-should-use-it-for-your-research]]&lt;br /&gt;
*[[Tutorials|Basic tutorial]]&lt;br /&gt;
*[[Running Docker locally and on Hydras]]&lt;br /&gt;
&lt;br /&gt;
=== Department computing resources ===&lt;br /&gt;
&lt;br /&gt;
==== Printing ====&lt;br /&gt;
*[[Department_Printers|Department Printers]] &lt;br /&gt;
&lt;br /&gt;
==== Web Hosting ====&lt;br /&gt;
#[[How to host your webpage under stat.cmu.edu]]&lt;br /&gt;
#[[Bulding your website with Hugo]]&lt;br /&gt;
&lt;br /&gt;
==== Servers ====&lt;br /&gt;
*[[Linux_and_department_servers|Department Servers]]. This wiki introduces you to the &#039;&#039;&#039;hydra&#039;&#039;&#039; servers, basic Linux commands, and more. &lt;br /&gt;
*#[[Terminal_multiplexers|Terminal multiplexers (screen and tmux)]]. This wiki presents tools for use on the &#039;&#039;hydra&#039;&#039; that can allow you to run jobs even when your not on the server, and much much more!&lt;br /&gt;
*#[[Python on the server]]. This wiki introduces uses command line approaches to running &amp;lt;code&amp;gt;python&amp;lt;/code&amp;gt; on a virtual machine, and how to use jupyter notebooks through the server as well.&lt;br /&gt;
*#[[R on the server]]. This wiki introduces uses command line approaches to running &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; on a virtual machine&lt;br /&gt;
&lt;br /&gt;
*[[Department GPU Servers]], for small jobs needing 1 GPU.&lt;br /&gt;
*[[GPU Cluster]], for larger jobs that may need multiple GPUs and many CPUs.&lt;br /&gt;
*[[PSC]], the Pittsburgh Supercomputing Center. Free accounts and usage credits are available &lt;br /&gt;
*#[[ACCESS]]&lt;br /&gt;
*#[[Slurm]]. This wiki introduces the job management system uses at PSC.&lt;br /&gt;
*#[[Parallelization_on_PSC/Bridges-2]]&lt;br /&gt;
*#[[Access PSC compute nodes directly from VSCode]]&lt;br /&gt;
*Azure&lt;br /&gt;
*#[[Azure_getting_started|Azure: getting started]]. This wiki walks you through the steps to create a virtual machine through Azure&lt;br /&gt;
*#[[How to obtain a virtual machine with a GPU]]&lt;br /&gt;
*#[[Attach a disk to a virtual machine]]&lt;br /&gt;
&lt;br /&gt;
=== Development guides ===&lt;br /&gt;
&lt;br /&gt;
====[[Parallel_computing|Parallelization]]====&lt;br /&gt;
*[[Parallelization_in_r|R-specific parallelization]]&lt;br /&gt;
*[[Parallelization_in_python|Python-specific parallelization]]&lt;br /&gt;
&lt;br /&gt;
==== Package development ====&lt;br /&gt;
*[[Developing_R_packages|R package development]]&lt;br /&gt;
*[[Developing_python_packages|Python package development]]&lt;br /&gt;
[[Terminal automation]]. This wiki introduces approaches to automate Linux/OS command line calls.&lt;br /&gt;
*[[Bash files]]. This wiki presents potential uses of Bash files including automated file/folder creation, parallelization and much more!&lt;br /&gt;
*[[Make]]. This wiki presents potential uses of &amp;lt;code&amp;gt;make&amp;lt;/code&amp;gt; and Makefiles including automated workflow and dependency tracking&lt;br /&gt;
*[[Jumping to frequently used folders and automating environment setups]]&lt;br /&gt;
&lt;br /&gt;
==== Version Control ====&lt;br /&gt;
See the guide provided in the site of [https://36-750.github.io/tools/version-control/ 36750 - Statistical Computing]&lt;br /&gt;
&lt;br /&gt;
==== Reference management ====&lt;br /&gt;
#Zotero &lt;br /&gt;
#Mendeley &lt;br /&gt;
&lt;br /&gt;
==== Integrated Development Environments (IDEs) ====&lt;br /&gt;
#[[VSCode|VSCode]] &lt;br /&gt;
#PyCharm &lt;br /&gt;
#RStudio &lt;br /&gt;
#Eclipse &lt;br /&gt;
&lt;br /&gt;
==== Text Editors ====&lt;br /&gt;
#Vim &lt;br /&gt;
#Nano &lt;br /&gt;
#Emacs &lt;br /&gt;
#Atom &lt;br /&gt;
#Sublime &lt;br /&gt;
&lt;br /&gt;
See also the guide provided in the site of [https://36-750.github.io/tools/editors/ 36750 - Statistical Computing]&lt;br /&gt;
&lt;br /&gt;
==== AI Tools ====&lt;br /&gt;
* [[ChatGPT and other LLMs]]&lt;br /&gt;
&lt;br /&gt;
*[[github_copilot|GitHub Copilot]]&lt;br /&gt;
&lt;br /&gt;
==== Miscellaneous====&lt;br /&gt;
#[[Windows_Subsystem_for_Linux_(WSL)_and_Python|Windows&amp;amp;nbsp;Subsystem for Linux (WSL) and Python]]&lt;br /&gt;
#[[Amphetamine|Amphetamine: How to keep your computer awake]]&lt;br /&gt;
&lt;br /&gt;
===General Department Computing Resources===&lt;br /&gt;
&lt;br /&gt;
*Fall 2021 [[Computing_seminar_resources|Computing Seminar Resources]] (video and slides)&lt;br /&gt;
&lt;br /&gt;
==Ideas for the Wiki==&lt;br /&gt;
&lt;br /&gt;
Please see the [[TODO|TODO page]] to post suggestions and errata.&lt;br /&gt;
&lt;br /&gt;
==Using the Wiki==&lt;br /&gt;
&lt;br /&gt;
*Here is some&amp;amp;nbsp;[[Generic_Wiki_Information|Generic Wiki Information]]&amp;amp;nbsp;that you may be interested in, such as a users&#039; guide and other resources for Wiki users.&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_r&amp;diff=221</id>
		<title>Parallelization in r</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_r&amp;diff=221"/>
		<updated>2025-07-01T14:19:02Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
There are a few ways to parallelize within &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;, generally broken up into 3 different approaches the capture different “for-loop” frameworks in &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. They include:&lt;br /&gt;
&lt;br /&gt;
#Parallelization in straight &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt;-loop-style with &amp;lt;code&amp;gt;foeach&amp;lt;/code&amp;gt;&lt;br /&gt;
#Parallelization in &amp;lt;code&amp;gt;apply&amp;lt;/code&amp;gt;-style loops with &amp;lt;code&amp;gt;mclapply&amp;lt;/code&amp;gt;&lt;br /&gt;
#Parallelization with &amp;lt;code&amp;gt;tidyverse&amp;lt;/code&amp;gt; nesting-style loops with &amp;lt;code&amp;gt;multidplyr&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The final option in the above list is still too experimental and limited to truly encourage.&lt;br /&gt;
&lt;br /&gt;
== General Starting Point ==&lt;br /&gt;
&lt;br /&gt;
To start parallelizing within &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; , we need to have some understanding of how many cores we have. &amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;library(parallel)&lt;br /&gt;
numCores &amp;lt;- detectCores()&lt;br /&gt;
numCores&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will tell you the total number of cores on your computer. We often recommend using less than the total number of cores when working on your personal computer or on the department servers to leave room for other jobs. You can always determine the current server system use by checking the hydra usage page.&lt;br /&gt;
&lt;br /&gt;
= for-loop style: &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;doParallel&amp;lt;/code&amp;gt; library (&amp;lt;code&amp;gt;foreach ()&amp;amp;nbsp;%dopar%&amp;lt;/code&amp;gt;) =&lt;br /&gt;
&lt;br /&gt;
The combination of &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;doParallel&amp;lt;/code&amp;gt; can convert for-loop style coding into parallel processing and provide a lot of additional parameters that can help reduce unnecessary memory usage, provide progress reports, and more.&lt;br /&gt;
&lt;br /&gt;
In the following presentation, we’ll demonstrate the speed obtained with different approaches using a simulation function defined below:&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;my_k &amp;lt;- rep(1:14, each = 20)&lt;br /&gt;
&lt;br /&gt;
my_simulation &amp;lt;- function(k) {&lt;br /&gt;
  x &amp;lt;- runif(30,-3, 3)&lt;br /&gt;
  y &amp;lt;- 2*x + 2* rnorm(n = 30)&lt;br /&gt;
  x0 &amp;lt;- runif(10000,-3, 3)&lt;br /&gt;
  y0 &amp;lt;- 2*x0 + 2*rnorm(n = 10000)&lt;br /&gt;
&lt;br /&gt;
  my_lm &amp;lt;- lm(y ~ poly(x,k))&lt;br /&gt;
  y0_hat &amp;lt;- predict(my_lm, data.frame(x = x0))&lt;br /&gt;
  return(mean((y0-y0_hat)^2))&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Single Core Analysis ==&lt;br /&gt;
&lt;br /&gt;
We naturally want to compare a parallelization approach with the base, single core analysis, which can be done using a standard for loop over various values of &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; :&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;#base for-loop&lt;br /&gt;
system.time({&lt;br /&gt;
  storage_f &amp;lt;- c()&lt;br /&gt;
  for (k in my_k) {&lt;br /&gt;
    storage_f &amp;lt;- c(storage_f, my_simulation(k))&lt;br /&gt;
  }&lt;br /&gt;
})&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   1.811   0.633   2.449&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; package, we can also use just a single core in the following way which results in approximately the same run time as the previous code segment:&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;#single-core `foreach`&lt;br /&gt;
library(foreach)&lt;br /&gt;
system.time({&lt;br /&gt;
  storage_fe &amp;lt;- foreach (k=my_k, .combine=c) %do% {&lt;br /&gt;
    my_simulation(k)&lt;br /&gt;
  }&lt;br /&gt;
})&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   1.876   0.640   2.522&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It should be clear that the &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; function works very similar to the base &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt;-loop; the difference in the syntax is mainly the fact that there is a &amp;lt;code&amp;gt;%do%&amp;lt;/code&amp;gt; before the opening bracket. Additionally, notice that the loops of &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; for values in &amp;lt;code&amp;gt;my_k&amp;lt;/code&amp;gt; is expressed differently than in a standard &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; loop (&amp;lt;code&amp;gt;k in my_k&amp;lt;/code&amp;gt; vs &amp;lt;code&amp;gt;k = my_k&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
== Multi-Core Analysis ==&lt;br /&gt;
&lt;br /&gt;
To use multiple cores with &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; we just change the above code to:&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;library(doParallel)&lt;br /&gt;
registerDoParallel(numCores-2)  # use multicore, set to the number of our cores&lt;br /&gt;
&lt;br /&gt;
system.time({&lt;br /&gt;
  storage_p &amp;lt;- foreach (k=my_k, .combine=c) %dopar% {&lt;br /&gt;
     my_simulation(k)&lt;br /&gt;
  }&lt;br /&gt;
})&lt;br /&gt;
stopImplicitCluster()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   3.513   2.520   0.601&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The last number under “elapsed” suggest that although we used more core + user time overall, we completed the overall process faster than without parallelization. The &amp;lt;code&amp;gt;%dopar%&amp;lt;/code&amp;gt; represents &amp;quot;do parallelization.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Additional Parameters ==&lt;br /&gt;
&lt;br /&gt;
#&amp;lt;code&amp;gt;.combine&amp;lt;/code&amp;gt;: this parameter tells the system how you’d like to return each step of the for-loop when combined at the end. Above we used &amp;lt;code&amp;gt;.combine = c&amp;lt;/code&amp;gt; to get a vector back, but we could also use things like &amp;lt;code&amp;gt;rbind&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cbind&amp;lt;/code&amp;gt;. Note that the default is a list.&lt;br /&gt;
#Memory and library passing: Sometimes you have a lot of functions or memory in your RAM before your run the parallelized for loop. The &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; function tries to reduce the amount memory used for each parallel process by constraining which functions are passed in. To specify which packages you’d like to use, use the &amp;lt;code&amp;gt;.packages&amp;lt;/code&amp;gt; command in &amp;lt;code&amp;gt;foreach.&amp;lt;/code&amp;gt; For example: &amp;lt;code&amp;gt;.packages = c(&amp;quot;dplyr&amp;quot;, &amp;quot;tidyr&amp;quot;)&amp;lt;/code&amp;gt;. Additionally, you can constrain what data is passed into each process with the parameter &amp;lt;code&amp;gt;.export&amp;lt;/code&amp;gt; which uses strings relative to your global environment.&lt;br /&gt;
#Progress bars: To have a running progress bar, one can use the package &amp;lt;code&amp;gt;doSNOW&amp;lt;/code&amp;gt;. For the previous process we can add a progress bar with the following set of code: &amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;library(doSNOW)&lt;br /&gt;
cl &amp;lt;- makeCluster(numCores-4)&lt;br /&gt;
registerDoSNOW(cl)&lt;br /&gt;
pb &amp;lt;- txtProgressBar(max = length(my_k), style = 3)&lt;br /&gt;
progress &amp;lt;- function(n) setTxtProgressBar(pb, n)&lt;br /&gt;
opts &amp;lt;- list(progress = progress)&lt;br /&gt;
&lt;br /&gt;
storage_p &amp;lt;- foreach (k=my_k, .combine=c,&lt;br /&gt;
                      .options.snow = opts) %dopar% {&lt;br /&gt;
   my_simulation(k)&lt;br /&gt;
}&lt;br /&gt;
parallel::stopCluster(cl)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;|==============================================================        | 83%&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
= lapply style: &amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt; library (&amp;lt;code&amp;gt;mclapply&amp;lt;/code&amp;gt;) =&lt;br /&gt;
&lt;br /&gt;
The second package we will look at is &amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt;, which includes the function &amp;lt;code&amp;gt;mclapply&amp;lt;/code&amp;gt; which can be used very similar to an &amp;lt;code&amp;gt;lapply&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Single Core Analysis ==&lt;br /&gt;
&lt;br /&gt;
For a single-core analysis of the same simulation seen in the previous section, we could run:&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;&lt;br /&gt;
system.time({&lt;br /&gt;
  results &amp;lt;- lapply(my_k, my_simulation)&lt;br /&gt;
  my_data &amp;lt;- data.frame(k = my_k,&lt;br /&gt;
                        values = unlist(results))&lt;br /&gt;
})&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   1.819   0.641   2.467&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Multi-Core Analysis ==&lt;br /&gt;
&lt;br /&gt;
Now, with the &amp;lt;code&amp;gt;mclapply&amp;lt;/code&amp;gt; function we can do basically the same thing as before. The difference here is the same as the difference between using &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;lapply&amp;lt;/code&amp;gt; in base &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;.  &amp;lt;code&amp;gt;mclapply&amp;lt;/code&amp;gt; applies each of the values in &amp;lt;code&amp;gt;my_k&amp;lt;/code&amp;gt; to the simulation, and parallelizes it across &amp;lt;code&amp;gt;numCores&amp;lt;/code&amp;gt; cores.&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;system.time({&lt;br /&gt;
  results &amp;lt;- mclapply(my_k, my_simulation, mc.cores = numCores)&lt;br /&gt;
  my_data &amp;lt;- data.frame(k = my_k,&lt;br /&gt;
                        values = unlist(results))&lt;br /&gt;
})&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   2.208   1.636   0.522&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Extra Resources: ==&lt;br /&gt;
&lt;br /&gt;
*[https://nceas.github.io/oss-lessons/parallel-computing-in-r/parallel-computing-in-r.html Matt Jones]&lt;br /&gt;
*[https://blog.dominodatalab.com/multicore-data-science-r-python/ R and python more options]&lt;br /&gt;
*&amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt; [https://stat.ethz.ch/R-manual/R-devel/library/parallel/doc/parallel.pdf manual]&lt;br /&gt;
*&amp;lt;code&amp;gt;doParallel&amp;lt;/code&amp;gt; + &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; [https://cran.r-project.org/web/packages/doParallel/vignettes/gettingstartedParallel.pdf vignette]&lt;br /&gt;
= The Fastest One as of February 2024:&amp;amp;nbsp;&amp;lt;code&amp;gt;BiocParallel&amp;lt;/code&amp;gt;=&lt;br /&gt;
&lt;br /&gt;
All of the above are good solutions, but from practical experience, we have seen that sometimes the previous methods can incur high overheads. A consistently faster solution comes from a package developed and maintained by the R bioinformatics community:&amp;amp;nbsp;&amp;amp;nbsp;&amp;lt;code&amp;gt;BioConductor&amp;lt;/code&amp;gt;. The package is named &amp;lt;code&amp;gt;BiocParallel&amp;amp;nbsp;&amp;lt;/code&amp;gt;and its interface is almost exactly the same as the one for the&amp;amp;nbsp;&amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt; package seen in the previous section. The only difference is that we need another package.&lt;br /&gt;
&lt;br /&gt;
First of all, install the &amp;lt;code&amp;gt;BiocParallel&amp;lt;/code&amp;gt; package manager via&lt;br /&gt;
&amp;lt;pre tabindex=&amp;quot;0&amp;quot;&amp;gt;install.packages(&amp;quot;BiocManager&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will make available an &amp;lt;code&amp;gt;install&amp;amp;nbsp;&amp;lt;/code&amp;gt;command, which we use as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;install(&amp;quot;BiocParallel&amp;quot;)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use a simple command. Assuming the function to run multiple times in parallel is named &amp;lt;code&amp;gt;foo&amp;lt;/code&amp;gt;, we type:&lt;br /&gt;
&amp;lt;pre tabindex=&amp;quot;0&amp;quot;&amp;gt;library(&#039;BiocParallel&#039;)&lt;br /&gt;
system.time( &lt;br /&gt;
     y &amp;lt;- bplapply(seq_len(100), foo) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See this [https://lcolladotor.github.io/2016/03/07/biocparallel/ article] for more information.&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_r&amp;diff=220</id>
		<title>Parallelization in r</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_r&amp;diff=220"/>
		<updated>2025-07-01T14:18:32Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: Updated and cleaned up the language on this page.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
There are a few ways to parallelize within &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;, generally broken up into 3 different approaches the capture different “for-loop” frameworks in &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. They include:&lt;br /&gt;
&lt;br /&gt;
#Parallelization in straight &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt;-loop-style with &amp;lt;code&amp;gt;foeach&amp;lt;/code&amp;gt;&lt;br /&gt;
#Parallelization in &amp;lt;code&amp;gt;apply&amp;lt;/code&amp;gt;-style loops with &amp;lt;code&amp;gt;mclapply&amp;lt;/code&amp;gt;&lt;br /&gt;
#Parallelization with &amp;lt;code&amp;gt;tidyverse&amp;lt;/code&amp;gt; nesting-style loops with &amp;lt;code&amp;gt;multidplyr&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The final option in the above list is still too experimental and limited to truly encourage.&lt;br /&gt;
&lt;br /&gt;
== General Starting Point ==&lt;br /&gt;
&lt;br /&gt;
To start parallelizing within &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; , we need to have some understanding of how many cores we have. &amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;library(parallel)&lt;br /&gt;
numCores &amp;lt;- detectCores()&lt;br /&gt;
numCores&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will tell you the total number of cores on your computer. We often recommend using less than the total number of cores when working on your personal computer or on the department servers to leave room for other jobs. You can always determine the current server system use by checking the hydra usage page.&lt;br /&gt;
&lt;br /&gt;
= for-loop style: &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;doParallel&amp;lt;/code&amp;gt; library (&amp;lt;code&amp;gt;foreach ()&amp;amp;nbsp;%dopar%&amp;lt;/code&amp;gt;) =&lt;br /&gt;
&lt;br /&gt;
The combination of &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;doParallel&amp;lt;/code&amp;gt; can convert for-loop style coding into parallel processing and provide a lot of additional parameters that can help reduce unnecessary memory usage, provide progress reports, and more.&lt;br /&gt;
&lt;br /&gt;
In the following presentation, we’ll demonstrate the speed obtained with different approaches using a simulation function defined below:&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;my_k &amp;lt;- rep(1:14, each = 20)&lt;br /&gt;
&lt;br /&gt;
my_simulation &amp;lt;- function(k) {&lt;br /&gt;
  x &amp;lt;- runif(30,-3, 3)&lt;br /&gt;
  y &amp;lt;- 2*x + 2* rnorm(n = 30)&lt;br /&gt;
  x0 &amp;lt;- runif(10000,-3, 3)&lt;br /&gt;
  y0 &amp;lt;- 2*x0 + 2*rnorm(n = 10000)&lt;br /&gt;
&lt;br /&gt;
  my_lm &amp;lt;- lm(y ~ poly(x,k))&lt;br /&gt;
  y0_hat &amp;lt;- predict(my_lm, data.frame(x = x0))&lt;br /&gt;
  return(mean((y0-y0_hat)^2))&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Single Core Analysis ==&lt;br /&gt;
&lt;br /&gt;
We naturally want to compare a parallelization approach with the base, single core analysis, which can be done using a standard for loop over various values of &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; :&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;#base for-loop&lt;br /&gt;
system.time({&lt;br /&gt;
  storage_f &amp;lt;- c()&lt;br /&gt;
  for (k in my_k) {&lt;br /&gt;
    storage_f &amp;lt;- c(storage_f, my_simulation(k))&lt;br /&gt;
  }&lt;br /&gt;
})&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   1.811   0.633   2.449&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; package, we can also use just a single core in the following way which results in approximately the same run time as the previous code segment:&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;#single-core `foreach`&lt;br /&gt;
library(foreach)&lt;br /&gt;
system.time({&lt;br /&gt;
  storage_fe &amp;lt;- foreach (k=my_k, .combine=c) %do% {&lt;br /&gt;
    my_simulation(k)&lt;br /&gt;
  }&lt;br /&gt;
})&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   1.876   0.640   2.522&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It should be clear that the &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; function works very similar to the base &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt;-loop; the difference in the syntax is mainly the fact that there is a &amp;lt;code&amp;gt;%do%&amp;lt;/code&amp;gt; before the opening bracket. Additionally, notice that the loops of &amp;lt;code&amp;gt;k&amp;lt;/code&amp;gt; for values in &amp;lt;code&amp;gt;my_k&amp;lt;/code&amp;gt; is expressed differently than in a standard &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; loop (&amp;lt;code&amp;gt;k in my_k&amp;lt;/code&amp;gt; vs &amp;lt;code&amp;gt;k = my_k&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
== Multi-Core Analysis ==&lt;br /&gt;
&lt;br /&gt;
To use multiple cores with &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; we just change the above code to:&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;library(doParallel)&lt;br /&gt;
registerDoParallel(numCores-2)  # use multicore, set to the number of our cores&lt;br /&gt;
&lt;br /&gt;
system.time({&lt;br /&gt;
  storage_p &amp;lt;- foreach (k=my_k, .combine=c) %dopar% {&lt;br /&gt;
     my_simulation(k)&lt;br /&gt;
  }&lt;br /&gt;
})&lt;br /&gt;
stopImplicitCluster()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   3.513   2.520   0.601&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The last number under “elapsed” suggest that although we used more core + user time overall, we completed the overall process faster than without parallelization. The &amp;lt;code&amp;gt;%dopar%&amp;lt;/code&amp;gt; represents &amp;quot;do parallelization.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Additional Parameters ==&lt;br /&gt;
&lt;br /&gt;
#&amp;lt;code&amp;gt;.combine&amp;lt;/code&amp;gt;: this parameter tells the system how you’d like to return each step of the for-loop when combined at the end. Above we used &amp;lt;code&amp;gt;.combine = c&amp;lt;/code&amp;gt; to get a vector back, but we could also use things like &amp;lt;code&amp;gt;rbind&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cbind&amp;lt;/code&amp;gt;. Note that the default is a list.&lt;br /&gt;
#Memory and library passing: Sometimes you have a lot of functions or memory in your RAM before your run the parallelized for loop. The &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; function tries to reduce the amount memory used for each parallel process by constraining which functions are passed in. To specify which packages you’d like to use, use the &amp;lt;code&amp;gt;.packages&amp;lt;/code&amp;gt; command in &amp;lt;code&amp;gt;foreach.&amp;lt;/code&amp;gt; For example: &amp;lt;code&amp;gt;.packages = c(&amp;quot;dplyr&amp;quot;, &amp;quot;tidyr&amp;quot;)&amp;lt;/code&amp;gt;. Additionally, you can constrain what data is passed into each process with the parameter &amp;lt;code&amp;gt;.export&amp;lt;/code&amp;gt; which uses strings relative to your global environment.&lt;br /&gt;
#Progress bars: To have a running progress bar, one can use the package &amp;lt;code&amp;gt;doSNOW&amp;lt;/code&amp;gt;. For the previous process we can add a progress bar with the following set of code: &amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;library(doSNOW)&lt;br /&gt;
cl &amp;lt;- makeCluster(numCores-4)&lt;br /&gt;
registerDoSNOW(cl)&lt;br /&gt;
pb &amp;lt;- txtProgressBar(max = length(my_k), style = 3)&lt;br /&gt;
progress &amp;lt;- function(n) setTxtProgressBar(pb, n)&lt;br /&gt;
opts &amp;lt;- list(progress = progress)&lt;br /&gt;
&lt;br /&gt;
storage_p &amp;lt;- foreach (k=my_k, .combine=c,&lt;br /&gt;
                      .options.snow = opts) %dopar% {&lt;br /&gt;
   my_simulation(k)&lt;br /&gt;
}&lt;br /&gt;
parallel::stopCluster(cl)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;|==============================================================        | 83%&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
= lapply style: &amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt; library (&amp;lt;code&amp;gt;mclapply&amp;lt;/code&amp;gt;) =&lt;br /&gt;
&lt;br /&gt;
The second package we will look at is &amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt;, which includes the function &amp;lt;code&amp;gt;mclapply&amp;lt;/code&amp;gt; which can be used very similar to an &amp;lt;code&amp;gt;lapply&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Single Core Analysis ==&lt;br /&gt;
&lt;br /&gt;
For a single-core analysis of the same simulation seen in the previous section, we could run:&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;&lt;br /&gt;
system.time({&lt;br /&gt;
  results &amp;lt;- lapply(my_k, my_simulation)&lt;br /&gt;
  my_data &amp;lt;- data.frame(k = my_k,&lt;br /&gt;
                        values = unlist(results))&lt;br /&gt;
})&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   1.819   0.641   2.467&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Multi-Core Analysis ==&lt;br /&gt;
&lt;br /&gt;
Now, with the &amp;lt;code&amp;gt;mclapply&amp;lt;/code&amp;gt; function we can do basically the same thing as before. The difference here is the same as the difference between using &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;lapply&amp;lt;/code&amp;gt; in base &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;.  &amp;lt;code&amp;gt;mclapply&amp;lt;/code&amp;gt; applies each of the values in &amp;lt;code&amp;gt;my_k&amp;lt;/code&amp;gt; to the simulation, and parallelizes it across &amp;lt;code&amp;gt;numCores&amp;lt;/code&amp;gt; cores.&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;system.time({&lt;br /&gt;
  results &amp;lt;- mclapply(my_k, my_simulation, mc.cores = numCores)&lt;br /&gt;
  my_data &amp;lt;- data.frame(k = my_k,&lt;br /&gt;
                        values = unlist(results))&lt;br /&gt;
})&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;r&amp;quot;&amp;gt;##    user  system elapsed&lt;br /&gt;
##   2.208   1.636   0.522&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Extra Resources: ==&lt;br /&gt;
&lt;br /&gt;
*[https://nceas.github.io/oss-lessons/parallel-computing-in-r/parallel-computing-in-r.html Matt Jones]&lt;br /&gt;
*[https://blog.dominodatalab.com/multicore-data-science-r-python/ R and python more options]&lt;br /&gt;
*&amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt; [https://stat.ethz.ch/R-manual/R-devel/library/parallel/doc/parallel.pdf manual]&lt;br /&gt;
*&amp;lt;code&amp;gt;doParallel&amp;lt;/code&amp;gt; + &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; [https://cran.r-project.org/web/packages/doParallel/vignettes/gettingstartedParallel.pdf vignette]&lt;br /&gt;
== The Fastest One as of February 2024:&amp;amp;nbsp;&amp;lt;code&amp;gt;BiocParallel&amp;lt;/code&amp;gt;==&lt;br /&gt;
&lt;br /&gt;
All of the above are good solutions, but from practical experience, we have seen that sometimes the previous methods can incur high overheads. A consistently faster solution comes from a package developed and maintained by the R bioinformatics community:&amp;amp;nbsp;&amp;amp;nbsp;&amp;lt;code&amp;gt;BioConductor&amp;lt;/code&amp;gt;. The package is named &amp;lt;code&amp;gt;BiocParallel&amp;amp;nbsp;&amp;lt;/code&amp;gt;and its interface is almost exactly the same as the one for the&amp;amp;nbsp;&amp;lt;code&amp;gt;parallel&amp;lt;/code&amp;gt; package seen in the previous section. The only difference is that we need another package.&lt;br /&gt;
&lt;br /&gt;
First of all, install the &amp;lt;code&amp;gt;BiocParallel&amp;lt;/code&amp;gt; package manager via&lt;br /&gt;
&amp;lt;pre tabindex=&amp;quot;0&amp;quot;&amp;gt;install.packages(&amp;quot;BiocManager&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will make available an &amp;lt;code&amp;gt;install&amp;amp;nbsp;&amp;lt;/code&amp;gt;command, which we use as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;install(&amp;quot;BiocParallel&amp;quot;)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can then use a simple command. Assuming the function to run multiple times in parallel is named &amp;lt;code&amp;gt;foo&amp;lt;/code&amp;gt;, we type:&lt;br /&gt;
&amp;lt;pre tabindex=&amp;quot;0&amp;quot;&amp;gt;library(&#039;BiocParallel&#039;)&lt;br /&gt;
system.time( &lt;br /&gt;
     y &amp;lt;- bplapply(seq_len(100), foo) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See this [https://lcolladotor.github.io/2016/03/07/biocparallel/ article] for more information.&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=219</id>
		<title>Parallelization in python</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=219"/>
		<updated>2025-07-01T14:01:29Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In Python, parallelization is a bit more of a “wild west” in terms of the number of parallelization packages available. This wiki will attempt to present minor examples of some common approaches, along with an outline of things to think about in selecting the library you end up using. In general, the process in Python involves:&lt;br /&gt;
&lt;br /&gt;
# defining a function that captures what you want to have parallelized&lt;br /&gt;
# calling that function repeatedly in the the parallelization package way (which often looks like a list-recursive call).&lt;br /&gt;
&lt;br /&gt;
There are a few different general frameworks to accomplish this.&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;General wrappers&#039;&#039;&#039; define the cores you’ll be using and then wrap that around the function call in a list-recursive way.&lt;br /&gt;
# &#039;&#039;&#039;Just in time compilers&#039;&#039;&#039;, in addition to providing parallelization tools, provide decorators (e.g. &amp;lt;code&amp;gt;@jit&amp;lt;/code&amp;gt;) for functions that allow for just in time compilation of the Python function into lower level machine code (like &amp;lt;code&amp;gt;C++&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
We will also discuss Python packages that are built specifically to parallelize jobs inside &#039;&#039;&#039;Jupyter notebooks&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
= Non-parallelized: Single Core Example =&lt;br /&gt;
&lt;br /&gt;
We naturally want to compare a parallelization approach with the base single core analysis, which can be done with the code as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;#base for-loop&lt;br /&gt;
import numpy as np&lt;br /&gt;
from sklearn.linear_model import LinearRegression&lt;br /&gt;
&lt;br /&gt;
my_k = list(np.arange(14)+1)*20&lt;br /&gt;
&lt;br /&gt;
def my_simulation(k):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    estamine the polynomial fit of degree k&#039;s MSE for a linear&lt;br /&gt;
    relationship&lt;br /&gt;
&lt;br /&gt;
    Parameters&lt;br /&gt;
    ----------&lt;br /&gt;
    k : int&lt;br /&gt;
    degree of polynomial fit&lt;br /&gt;
&lt;br /&gt;
    Returns&lt;br /&gt;
    -------&lt;br /&gt;
    mse of fit based on 30 points applied to a test set of 10000 points&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    x = np.random.uniform(size=30,low=-3,high=3)&lt;br /&gt;
    y = 2*x + 2* np.random.normal(size=30)&lt;br /&gt;
    y = y.reshape(-1,1)&lt;br /&gt;
    X_poly = np.concatenate([x.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    x0 = np.random.uniform(size=10000,low=-3,high=3)&lt;br /&gt;
    y0 = 2*x0 + 2*np.random.normal(size=10000)&lt;br /&gt;
    y0 = y0.reshape(-1,1)&lt;br /&gt;
    X0_poly = np.concatenate([x0.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
    my_lm = LinearRegression().fit(X_poly,y)&lt;br /&gt;
    y0_hat = my_lm.predict(X0_poly)&lt;br /&gt;
&lt;br /&gt;
    return(np.mean((y0-y0_hat)**2))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import time &lt;br /&gt;
&lt;br /&gt;
starttime = time.time()&lt;br /&gt;
[my_simulation(k) for k in my_k]&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.9454693794250488 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= General Python Parallelization Packages =&lt;br /&gt;
&lt;br /&gt;
As previously mentioned, there are a lot of packages for general parallelization. Two commonly used ones are &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; looks a bit more like list-recursion and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; uses functions like &amp;lt;code&amp;gt;.map()&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; package is actually built into Python, and works in a pretty straightforward manner. It also allows for synchronous vs. asynchronous parallelization, where synchronous means the order of the jobs must be followed, one after the next, and asynchronous means multiple tasks can be executed in small parts in overlapping periods. Note that many statistical uses of parallelization do not need synchronous parallelization.&lt;br /&gt;
&lt;br /&gt;
Since both approaches have the same syntax we’ll be using &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; (as it works with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebooks&amp;lt;/code&amp;gt;). &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; can have problems with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s. In &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s, if you want to use &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; , it needs to be wrapped in &amp;lt;code&amp;gt;if name == &amp;quot;main&amp;quot;&amp;lt;/code&amp;gt; to properly fork processes; you can also save your process is as a .py file and import it into your &amp;lt;code&amp;gt;jupyter notebook.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
First, you need to identify the number of cores you have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import multiprocess as mp &lt;br /&gt;
#Could also using import multiprocessing as mp; the syntax is the same&lt;br /&gt;
&lt;br /&gt;
num_cores = mp.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Synchronous  ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;starttime = time.time()&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
  results = p.map(my_simulation, my_k)&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.7638578414916992 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Asynchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;starttime = time.time()&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
    results = p.map_async(my_simulation, my_k).get() &lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&lt;br /&gt;
    &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.8183681964874268 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
== &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; package is pretty straightforward and requires you to structure things in terms of list-recursion with a few wrappers to help the process work as desired. Just like with &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;, the number of cores needs to be computed before proceeding.&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import joblib&lt;br /&gt;
&lt;br /&gt;
num_cores = joblib.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;To use &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; functionality, we can import the functions &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; creates an instance for parallelization with a set number of jobs based on the core availability.  &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt; creates deferred function calls to distribute computations across multiple cores; this is the function that allows for simultaneous task execution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from joblib import Parallel, delayed&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
We then can move our list-recursive example into our &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; structure. We wrap &amp;lt;code&amp;gt;Parallel(...)()&amp;lt;/code&amp;gt; around the inside of the list-recursion and &amp;lt;code&amp;gt;delayed()&amp;lt;/code&amp;gt; around the functions that will be repeated multiple times.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;starttime = time.time()&lt;br /&gt;
r = Parallel(n_jobs=num_cores-2)(delayed(my_simulation)(k) for k in my_k)&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.5306367874145508 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Add-ons ===&lt;br /&gt;
&lt;br /&gt;
There are multiple add-ons that can be desirable&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;Verbosity&#039;&#039;&#039; can be added with a single parameter use (as seen below). This will provide more detailed output information.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;r = Parallel(n_jobs=num_cores-2, verbose=10)(delayed(my_simulation)(k) for k in my_k)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;Multiple parameters&#039;&#039;&#039; can be passed with redefinitions of functions to take groupings. To replace multiple for-loops, often the function &amp;lt;code&amp;gt;itertools.product()&amp;lt;/code&amp;gt; is used for the parameters.&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The package &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; is an open source project for parallel and distributed computing in Python. &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; is often preferred over &amp;lt;code&amp;gt;multiprocess/multiprocessing&amp;lt;/code&amp;gt; because of its efficient machine error handling methodology and its ability to handle larger data types. &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; allows for aggregation of results/values, meaning the output of one task can be used as input for subsequent tasks seamlessly. The implementation for &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; is not dissimilar from that of &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import ray&lt;br /&gt;
import os&amp;lt;/syntaxhighlight&amp;gt;In order to parallelize computation with &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt;, you must first initialize an instance of &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt;. Just like with &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;, the instance relies on the number of CPUs available.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
ray.init(num_cpus = os.cpu_count())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;Additionally, in order for &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; to work, the function &amp;lt;code&amp;gt;my_simulation&amp;lt;/code&amp;gt; needs to have the decorator &amp;lt;code&amp;gt;@ray.remote&amp;lt;/code&amp;gt; prior to its heading. &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
@ray.remote&lt;br /&gt;
def my_simulation(k):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;the rest of the function code goes here&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;Once we have initialized an instance based on our number of available CPUs, and we have made sure our function has the proper decorator, we can parallelize computation as follows. The &amp;lt;code&amp;gt;.remote()&amp;lt;/code&amp;gt; passes the variable into the function, even if the previous computation has not finished running, and the &amp;lt;code&amp;gt;.get()&amp;lt;/code&amp;gt; compiles the results once finished.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
starttime = time.time()&lt;br /&gt;
futures = [my_simulation.remote(k) for k in my_k]&lt;br /&gt;
results = ray.get(futures)&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This &amp;lt;code&amp;gt;.remote()&amp;lt;/code&amp;gt; piece is useful because you can pass previous computations into it in order to have a parallelized, autoregressive work flow.&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
0.13494515419006348 seconds&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Jupyter Notebook: &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt;=&lt;br /&gt;
&lt;br /&gt;
For those that heavily use Jupyter notebooks, the package &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; works quite nicely. Some benefits of this package include its verbosity, its ability to serialize numpy arrays cleanly, and the fact that it can distribute across multiple machines. This package also works with &amp;lt;code&amp;gt;numba.jit&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Before opening your &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; you’ll want to make sure the extension is enabled - which can be done from the command line with the following command or inside the &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; extensions tab.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;ipcluster nbextension enable&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
You’ll then need to go to the “IPython Clusters” tab in &amp;lt;code&amp;gt;jupyter noteook&amp;lt;/code&amp;gt; and start some parallel clusters.&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; parallelization ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import ipyparallel as ipp&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
For direct execution we define &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; and provide it with what it needs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;rc = ipp.Client()&lt;br /&gt;
dv = rc[:] # direct view&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Passing Things to Each Cluster ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; provides ways for the user to pass only the necessary items to the the clusters as to not overload them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Passing packages all together&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;with dv.sync_imports():&lt;br /&gt;
    from sklearn.linear_model import LinearRegression&lt;br /&gt;
    # getting dependencies on the clusters&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Passing variables:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Below is just an example - we don’t need to pass &amp;lt;code&amp;gt;my_k&amp;lt;/code&amp;gt; since &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; will directly get it when we want to use it.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;dv[&amp;quot;my_k&amp;quot;] = my_k&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;We can also use the &amp;lt;code&amp;gt;%px&amp;lt;/code&amp;gt; magick to run lines of code on all servers.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%px import numpy as np&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Actual Functionality ===&lt;br /&gt;
&lt;br /&gt;
The actual functionality is very similar to &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;maps&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;apply&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Synchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_sync(my_simulation, my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;201 ms ± 34.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Asynchronous ====&lt;br /&gt;
For asynchronous, you need to use &amp;lt;code&amp;gt;.get()&amp;lt;/code&amp;gt; in order to get the results at a later time/when they come in, since the work is not done in order and is instead chunked for efficiency.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_async(my_simulation, my_k).get() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;209 ms ± 28.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= Just in Time Compilers + Parallelization =&lt;br /&gt;
&lt;br /&gt;
Just-in-time compilation is the compiling of your computer code during execution instead of beforehand. This means bytecode is not translated to machine code until run time, and is then executed directly. Code is continuously analyzed to determine where compilation/recompilation would speed up performance. Used in conjunction with parallelization, this is an efficient way to improve computational speed.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; is a high performance Python compiler that translates Python/numpy code into fast, efficient machine code. Python is traditionally regarded as a &amp;quot;slow&amp;quot; language; using &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt;, you can approach the speeds of languages such as C. &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; is specifically designed to work well with numpy arrays.&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
Similar to &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; requires a decorator &amp;lt;code&amp;gt;@njit (parallel = True)&amp;lt;/code&amp;gt; before the function. We will define a different function to be parallelized here to demonstrate &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;prange&amp;lt;/code&amp;gt; which allows for efficient parallelization across loops.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
A = np.random.rand(3, 3)&lt;br /&gt;
B = np.random.rand(3, 3)&lt;br /&gt;
&lt;br /&gt;
@njit(parallel = True)&lt;br /&gt;
def my_simulation(A, B):&lt;br /&gt;
    C = np.empty_like(A)&lt;br /&gt;
    for i in prange(A.shape[0]):&lt;br /&gt;
        for j in range(A.shape[1]):&lt;br /&gt;
            C[i, j] = A[i, j] + B[i, j]&lt;br /&gt;
    return C&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;code&amp;gt;prange&amp;lt;/code&amp;gt; should not be nested and should only ever be used in the outer loop. This is a very simple example, but for computationally complex looping problems, &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; is highly efficient. However, you need to ensure that you are not creating or declaring a variable within a loop that cannot be accessed outside of the loop, or else parallelization will not work.&lt;br /&gt;
&lt;br /&gt;
= Additional Topics and Resources =&lt;br /&gt;
&lt;br /&gt;
# [https://people.duke.edu/~ccc14/sta-663-2018/notebooks/S14A_Parallel_Programming_Introduction.html Duke’s statistical computing course] provides a set of examples for multiple Python parallelization packages&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=218</id>
		<title>Parallelization in python</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=218"/>
		<updated>2025-06-30T18:30:57Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: /* Just in Time compilers + parallelization */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;This article is still being developed - released earlier to align with Department seminar.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In Python, parallelization is a bit more of a “wild west” in terms of the number of parallelization packages available. This wiki will attempt to present minor examples of some common approaches, along with an outline of things to think about in selecting the library you end up using. In general, the process in Python involves:&lt;br /&gt;
&lt;br /&gt;
# defining a function that captures what you want to have parallelized&lt;br /&gt;
# calling that function repeatedly in the the parallelization package way (which often looks like a list-recursive call).&lt;br /&gt;
&lt;br /&gt;
There are a few different general frameworks to accomplish this.&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;General wrappers&#039;&#039;&#039; define the cores you’ll be using and then wrap that around the function call in a list-recursive way.&lt;br /&gt;
# &#039;&#039;&#039;Just in time compilers&#039;&#039;&#039;, in addition to providing parallelization tools, provide decorators (e.g. &amp;lt;code&amp;gt;@jit&amp;lt;/code&amp;gt;) for functions that allow for just in time compilation of the Python function into lower level machine code (like &amp;lt;code&amp;gt;C++&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
We will also discuss Python packages that are built specifically to parallelize jobs inside &#039;&#039;&#039;Jupyter notebooks&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
= Non-parallelized: Single Core Example =&lt;br /&gt;
&lt;br /&gt;
We naturally want to compare a parallelization approach with the base single core analysis, which can be done with the code as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;#base for-loop&lt;br /&gt;
import numpy as np&lt;br /&gt;
from sklearn.linear_model import LinearRegression&lt;br /&gt;
&lt;br /&gt;
my_k = list(np.arange(14)+1)*20&lt;br /&gt;
&lt;br /&gt;
def my_simulation(k):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    estamine the polynomial fit of degree k&#039;s MSE for a linear&lt;br /&gt;
    relationship&lt;br /&gt;
&lt;br /&gt;
    Parameters&lt;br /&gt;
    ----------&lt;br /&gt;
    k : int&lt;br /&gt;
    degree of polynomial fit&lt;br /&gt;
&lt;br /&gt;
    Returns&lt;br /&gt;
    -------&lt;br /&gt;
    mse of fit based on 30 points applied to a test set of 10000 points&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    x = np.random.uniform(size=30,low=-3,high=3)&lt;br /&gt;
    y = 2*x + 2* np.random.normal(size=30)&lt;br /&gt;
    y = y.reshape(-1,1)&lt;br /&gt;
    X_poly = np.concatenate([x.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    x0 = np.random.uniform(size=10000,low=-3,high=3)&lt;br /&gt;
    y0 = 2*x0 + 2*np.random.normal(size=10000)&lt;br /&gt;
    y0 = y0.reshape(-1,1)&lt;br /&gt;
    X0_poly = np.concatenate([x0.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
    my_lm = LinearRegression().fit(X_poly,y)&lt;br /&gt;
    y0_hat = my_lm.predict(X0_poly)&lt;br /&gt;
&lt;br /&gt;
    return(np.mean((y0-y0_hat)**2))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import time &lt;br /&gt;
&lt;br /&gt;
starttime = time.time()&lt;br /&gt;
[my_simulation(k) for k in my_k]&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.9454693794250488 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= General Python Parallelization Packages =&lt;br /&gt;
&lt;br /&gt;
As previously mentioned, there are a lot of packages for general parallelization. Two commonly used ones are &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; looks a bit more like list-recursion and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; uses functions like &amp;lt;code&amp;gt;.map()&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; package is actually built into Python, and works in a pretty straightforward manner. It also allows for synchronous vs. asynchronous parallelization, where synchronous means the order of the jobs must be followed, one after the next, and asynchronous means multiple tasks can be executed in small parts in overlapping periods. Note that many statistical uses of parallelization do not need synchronous parallelization.&lt;br /&gt;
&lt;br /&gt;
Since both approaches have the same syntax we’ll be using &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; (as it works with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebooks&amp;lt;/code&amp;gt;). &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; can have problems with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s. In &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s, if you want to use &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; , it needs to be wrapped in &amp;lt;code&amp;gt;if name == &amp;quot;main&amp;quot;&amp;lt;/code&amp;gt; to properly fork processes; you can also save your process is as a .py file and import it into your &amp;lt;code&amp;gt;jupyter notebook.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
First, you need to identify the number of cores you have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import multiprocess as mp &lt;br /&gt;
#Could also using import multiprocessing as mp; the syntax is the same&lt;br /&gt;
&lt;br /&gt;
num_cores = mp.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Synchronous  ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;starttime = time.time()&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
  results = p.map(my_simulation, my_k)&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.7638578414916992 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Asynchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;starttime = time.time()&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
    results = p.map_async(my_simulation, my_k).get() &lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&lt;br /&gt;
    &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.8183681964874268 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
== &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; package is pretty straightforward and requires you to structure things in terms of list-recursion with a few wrappers to help the process work as desired. Just like with &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;, the number of cores needs to be computed before proceeding.&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import joblib&lt;br /&gt;
&lt;br /&gt;
num_cores = joblib.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;To use &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; functionality, we can import the functions &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; creates an instance for parallelization with a set number of jobs based on the core availability.  &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt; creates deferred function calls to distribute computations across multiple cores; this is the function that allows for simultaneous task execution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from joblib import Parallel, delayed&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
We then can move our list-recursive example into our &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; structure. We wrap &amp;lt;code&amp;gt;Parallel(...)()&amp;lt;/code&amp;gt; around the inside of the list-recursion and &amp;lt;code&amp;gt;delayed()&amp;lt;/code&amp;gt; around the functions that will be repeated multiple times.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;starttime = time.time()&lt;br /&gt;
r = Parallel(n_jobs=num_cores-2)(delayed(my_simulation)(k) for k in my_k)&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.5306367874145508 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Add-ons ===&lt;br /&gt;
&lt;br /&gt;
There are multiple add-ons that can be desirable&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;Verbosity&#039;&#039;&#039; can be added with a single parameter use (as seen below). This will provide more detailed output information.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;r = Parallel(n_jobs=num_cores-2, verbose=10)(delayed(my_simulation)(k) for k in my_k)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;Multiple parameters&#039;&#039;&#039; can be passed with redefinitions of functions to take groupings. To replace multiple for-loops, often the function &amp;lt;code&amp;gt;itertools.product()&amp;lt;/code&amp;gt; is used for the parameters.&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The package &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; is an open source project for parallel and distributed computing in Python. &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; is often preferred over &amp;lt;code&amp;gt;multiprocess/multiprocessing&amp;lt;/code&amp;gt; because of its efficient machine error handling methodology and its ability to handle larger data types. &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; allows for aggregation of results/values, meaning the output of one task can be used as input for subsequent tasks seamlessly. The implementation for &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; is not dissimilar from that of &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import ray&lt;br /&gt;
import os&amp;lt;/syntaxhighlight&amp;gt;In order to parallelize computation with &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt;, you must first initialize an instance of &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt;. Just like with &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;, the instance relies on the number of CPUs available.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
ray.init(num_cpus = os.cpu_count())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;Additionally, in order for &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; to work, the function &amp;lt;code&amp;gt;my_simulation&amp;lt;/code&amp;gt; needs to have the decorator &amp;lt;code&amp;gt;@ray.remote&amp;lt;/code&amp;gt; prior to its heading. &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
@ray.remote&lt;br /&gt;
def my_simulation(k):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;the rest of the function code goes here&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;Once we have initialized an instance based on our number of available CPUs, and we have made sure our function has the proper decorator, we can parallelize computation as follows. The &amp;lt;code&amp;gt;.remote()&amp;lt;/code&amp;gt; passes the variable into the function, even if the previous computation has not finished running, and the &amp;lt;code&amp;gt;.get()&amp;lt;/code&amp;gt; compiles the results once finished.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
starttime = time.time()&lt;br /&gt;
futures = [my_simulation.remote(k) for k in my_k]&lt;br /&gt;
results = ray.get(futures)&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This &amp;lt;code&amp;gt;.remote()&amp;lt;/code&amp;gt; piece is useful because you can pass previous computations into it in order to have a parallelized, autoregressive work flow.&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
0.13494515419006348 seconds&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Jupyter Notebook: &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt;=&lt;br /&gt;
&lt;br /&gt;
For those that heavily use Jupyter notebooks, the package &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; works quite nicely. Some benefits of this package include its verbosity, its ability to serialize numpy arrays cleanly, and the fact that it can distribute across multiple machines. This package also works with &amp;lt;code&amp;gt;numba.jit&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Before opening your &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; you’ll want to make sure the extension is enabled - which can be done from the command line with the following command or inside the &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; extensions tab.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;ipcluster nbextension enable&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
You’ll then need to go to the “IPython Clusters” tab in &amp;lt;code&amp;gt;jupyter noteook&amp;lt;/code&amp;gt; and start some parallel clusters.&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; parallelization ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import ipyparallel as ipp&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
For direct execution we define &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; and provide it with what it needs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;rc = ipp.Client()&lt;br /&gt;
dv = rc[:] # direct view&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Passing Things to Each Cluster ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; provides ways for the user to pass only the necessary items to the the clusters as to not overload them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Passing packages all together&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;with dv.sync_imports():&lt;br /&gt;
    from sklearn.linear_model import LinearRegression&lt;br /&gt;
    # getting dependencies on the clusters&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Passing variables:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Below is just an example - we don’t need to pass &amp;lt;code&amp;gt;my_k&amp;lt;/code&amp;gt; since &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; will directly get it when we want to use it.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;dv[&amp;quot;my_k&amp;quot;] = my_k&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;We can also use the &amp;lt;code&amp;gt;%px&amp;lt;/code&amp;gt; magick to run lines of code on all servers.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%px import numpy as np&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Actual Functionality ===&lt;br /&gt;
&lt;br /&gt;
The actual functionality is very similar to &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;maps&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;apply&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Synchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_sync(my_simulation, my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;201 ms ± 34.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Asynchronous ====&lt;br /&gt;
For asynchronous, you need to use &amp;lt;code&amp;gt;.get()&amp;lt;/code&amp;gt; in order to get the results at a later time/when they come in, since the work is not done in order and is instead chunked for efficiency.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_async(my_simulation, my_k).get() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;209 ms ± 28.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= Just in Time Compilers + Parallelization =&lt;br /&gt;
&lt;br /&gt;
Just-in-time compilation is the compiling of your computer code during execution instead of beforehand. This means bytecode is not translated to machine code until run time, and is then executed directly. Code is continuously analyzed to determine where compilation/recompilation would speed up performance. Used in conjunction with parallelization, this is an efficient way to improve computational speed.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; is a high performance Python compiler that translates Python/numpy code into fast, efficient machine code. Python is traditionally regarded as a &amp;quot;slow&amp;quot; language; using &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt;, you can approach the speeds of languages such as C. &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; is specifically designed to work well with numpy arrays.&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
Similar to &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; requires a decorator &amp;lt;code&amp;gt;@njit (parallel = True)&amp;lt;/code&amp;gt; before the function. We will define a different function to be parallelized here to demonstrate &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;prange&amp;lt;/code&amp;gt; which allows for efficient parallelization across loops.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
A = np.random.rand(3, 3)&lt;br /&gt;
B = np.random.rand(3, 3)&lt;br /&gt;
&lt;br /&gt;
@njit(parallel = True)&lt;br /&gt;
def my_simulation(A, B):&lt;br /&gt;
    C = np.empty_like(A)&lt;br /&gt;
    for i in prange(A.shape[0]):&lt;br /&gt;
        for j in range(A.shape[1]):&lt;br /&gt;
            C[i, j] = A[i, j] + B[i, j]&lt;br /&gt;
    return C&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;code&amp;gt;prange&amp;lt;/code&amp;gt; should not be nested and should only ever be used in the outer loop. This is a very simple example, but for computationally complex looping problems, &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; is highly efficient. However, you need to ensure that you are not creating or declaring a variable within a loop that cannot be accessed outside of the loop, or else parallelization will not work.&lt;br /&gt;
&lt;br /&gt;
= Additional Topics and Resources =&lt;br /&gt;
&lt;br /&gt;
# [https://ray.io/ &amp;lt;code&amp;gt;Ray&amp;lt;/code&amp;gt; python package] allows paralleilization across multiple computers&lt;br /&gt;
# [https://people.duke.edu/~ccc14/sta-663-2018/notebooks/S14A_Parallel_Programming_Introduction.html Duke’s statistical computing course] provides a set of examples for multiple python parallelization packages |&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=217</id>
		<title>Parallelization in python</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=217"/>
		<updated>2025-06-25T17:29:21Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: Added a section on ray&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;This article is still being developed - released earlier to align with Department seminar.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In Python, parallelization is a bit more of a “wild west” in terms of the number of parallelization packages available. This wiki will attempt to present minor examples of some common approaches, along with an outline of things to think about in selecting the library you end up using. In general, the process in Python involves:&lt;br /&gt;
&lt;br /&gt;
# defining a function that captures what you want to have parallelized&lt;br /&gt;
# calling that function repeatedly in the the parallelization package way (which often looks like a list-recursive call).&lt;br /&gt;
&lt;br /&gt;
There are a few different general frameworks to accomplish this.&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;General wrappers&#039;&#039;&#039; define the cores you’ll be using and then wrap that around the function call in a list-recursive way.&lt;br /&gt;
# &#039;&#039;&#039;Just in time compilers&#039;&#039;&#039;, in addition to providing parallelization tools, provide decorators (e.g. &amp;lt;code&amp;gt;@jit&amp;lt;/code&amp;gt;) for functions that allow for just in time compilation of the Python function into lower level machine code (like &amp;lt;code&amp;gt;C++&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
We will also discuss Python packages that are built specifically to parallelize jobs inside &#039;&#039;&#039;Jupyter notebooks&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
= Non-parallelized: Single Core Example =&lt;br /&gt;
&lt;br /&gt;
We naturally want to compare a parallelization approach with the base single core analysis, which can be done with the code as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;#base for-loop&lt;br /&gt;
import numpy as np&lt;br /&gt;
from sklearn.linear_model import LinearRegression&lt;br /&gt;
&lt;br /&gt;
my_k = list(np.arange(14)+1)*20&lt;br /&gt;
&lt;br /&gt;
def my_simulation(k):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    estamine the polynomial fit of degree k&#039;s MSE for a linear&lt;br /&gt;
    relationship&lt;br /&gt;
&lt;br /&gt;
    Parameters&lt;br /&gt;
    ----------&lt;br /&gt;
    k : int&lt;br /&gt;
    degree of polynomial fit&lt;br /&gt;
&lt;br /&gt;
    Returns&lt;br /&gt;
    -------&lt;br /&gt;
    mse of fit based on 30 points applied to a test set of 10000 points&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    x = np.random.uniform(size=30,low=-3,high=3)&lt;br /&gt;
    y = 2*x + 2* np.random.normal(size=30)&lt;br /&gt;
    y = y.reshape(-1,1)&lt;br /&gt;
    X_poly = np.concatenate([x.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    x0 = np.random.uniform(size=10000,low=-3,high=3)&lt;br /&gt;
    y0 = 2*x0 + 2*np.random.normal(size=10000)&lt;br /&gt;
    y0 = y0.reshape(-1,1)&lt;br /&gt;
    X0_poly = np.concatenate([x0.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
    my_lm = LinearRegression().fit(X_poly,y)&lt;br /&gt;
    y0_hat = my_lm.predict(X0_poly)&lt;br /&gt;
&lt;br /&gt;
    return(np.mean((y0-y0_hat)**2))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import time &lt;br /&gt;
&lt;br /&gt;
starttime = time.time()&lt;br /&gt;
[my_simulation(k) for k in my_k]&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.9454693794250488 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= General Python Parallelization Packages =&lt;br /&gt;
&lt;br /&gt;
As previously mentioned, there are a lot of packages for general parallelization. Two commonly used ones are &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; looks a bit more like list-recursion and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; uses functions like &amp;lt;code&amp;gt;.map()&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; package is actually built into Python, and works in a pretty straightforward manner. It also allows for synchronous vs. asynchronous parallelization, where synchronous means the order of the jobs must be followed, one after the next, and asynchronous means multiple tasks can be executed in small parts in overlapping periods. Note that many statistical uses of parallelization do not need synchronous parallelization.&lt;br /&gt;
&lt;br /&gt;
Since both approaches have the same syntax we’ll be using &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; (as it works with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebooks&amp;lt;/code&amp;gt;). &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; can have problems with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s. In &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s, if you want to use &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; , it needs to be wrapped in &amp;lt;code&amp;gt;if name == &amp;quot;main&amp;quot;&amp;lt;/code&amp;gt; to properly fork processes; you can also save your process is as a .py file and import it into your &amp;lt;code&amp;gt;jupyter notebook.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
First, you need to identify the number of cores you have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import multiprocess as mp &lt;br /&gt;
#Could also using import multiprocessing as mp; the syntax is the same&lt;br /&gt;
&lt;br /&gt;
num_cores = mp.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Synchronous  ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;starttime = time.time()&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
  results = p.map(my_simulation, my_k)&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.7638578414916992 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Asynchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;starttime = time.time()&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
    results = p.map_async(my_simulation, my_k).get() &lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&lt;br /&gt;
    &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.8183681964874268 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
== &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; package is pretty straightforward and requires you to structure things in terms of list-recursion with a few wrappers to help the process work as desired. Just like with &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;, the number of cores needs to be computed before proceeding.&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import joblib&lt;br /&gt;
&lt;br /&gt;
num_cores = joblib.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;To use &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; functionality, we can import the functions &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; creates an instance for parallelization with a set number of jobs based on the core availability.  &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt; creates deferred function calls to distribute computations across multiple cores; this is the function that allows for simultaneous task execution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from joblib import Parallel, delayed&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
We then can move our list-recursive example into our &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; structure. We wrap &amp;lt;code&amp;gt;Parallel(...)()&amp;lt;/code&amp;gt; around the inside of the list-recursion and &amp;lt;code&amp;gt;delayed()&amp;lt;/code&amp;gt; around the functions that will be repeated multiple times.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;starttime = time.time()&lt;br /&gt;
r = Parallel(n_jobs=num_cores-2)(delayed(my_simulation)(k) for k in my_k)&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.5306367874145508 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Add-ons ===&lt;br /&gt;
&lt;br /&gt;
There are multiple add-ons that can be desirable&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;Verbosity&#039;&#039;&#039; can be added with a single parameter use (as seen below). This will provide more detailed output information.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;r = Parallel(n_jobs=num_cores-2, verbose=10)(delayed(my_simulation)(k) for k in my_k)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;Multiple parameters&#039;&#039;&#039; can be passed with redefinitions of functions to take groupings. To replace multiple for-loops, often the function &amp;lt;code&amp;gt;itertools.product()&amp;lt;/code&amp;gt; is used for the parameters.&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; ==&lt;br /&gt;
The package &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; is an open source project for parallel and distributed computing in Python. &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; is often preferred over &amp;lt;code&amp;gt;multiprocess/multiprocessing&amp;lt;/code&amp;gt; because of its efficient machine error handling methodology and its ability to handle larger data types. &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; allows for aggregation of results/values, meaning the output of one task can be used as input for subsequent tasks seamlessly. The implementation for &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; is not dissimilar from that of &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import ray&lt;br /&gt;
import os&amp;lt;/syntaxhighlight&amp;gt;In order to parallelize computation with &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt;, you must first initialize an instance of &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt;. Just like with &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;, the instance relies on the number of CPUs available.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
ray.init(num_cpus = os.cpu_count())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;Additionally, in order for &amp;lt;code&amp;gt;ray&amp;lt;/code&amp;gt; to work, the function &amp;lt;code&amp;gt;my_simulation&amp;lt;/code&amp;gt; needs to have the docker &amp;lt;code&amp;gt;@ray.remote&amp;lt;/code&amp;gt; prior to its heading. &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
@ray.remote&lt;br /&gt;
def my_simulation(k):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;the rest of the function code goes here&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;Once we have initialized an instance based on our number of available CPUs, and we have made sure our function has the proper docker, we can parallelize computation as follows. The &amp;lt;code&amp;gt;.remote()&amp;lt;/code&amp;gt; passes the variable into the function, even if the previous computation has not finished running, and the &amp;lt;code&amp;gt;.get()&amp;lt;/code&amp;gt; compiles the results once finished.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
starttime = time.time()&lt;br /&gt;
futures = [my_simulation.remote(k) for k in my_k]&lt;br /&gt;
results = ray.get(futures)&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This &amp;lt;code&amp;gt;.remote()&amp;lt;/code&amp;gt; piece is useful because you can pass previous computations into it in order to have a parallelized, autoregressive work flow.&amp;lt;syntaxhighlight&amp;gt;&lt;br /&gt;
0.13494515419006348 seconds&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Jupyter Notebook: &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt;=&lt;br /&gt;
&lt;br /&gt;
For those that heavily use Jupyter notebooks, the package &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; works quite nicely. Some benefits of this package include its verbosity, its ability to serialize numpy arrays cleanly, and the fact that it can distribute across multiple machines. This package also works with &amp;lt;code&amp;gt;numba.jit&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Before opening your &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; you’ll want to make sure the extension is enabled - which can be done from the command line with the following command or inside the &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; extensions tab.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;ipcluster nbextension enable&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
You’ll then need to go to the “IPython Clusters” tab in &amp;lt;code&amp;gt;jupyter noteook&amp;lt;/code&amp;gt; and start some parallel clusters.&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; parallelization ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import ipyparallel as ipp&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
For direct execution we define &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; and provide it with what it needs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;rc = ipp.Client()&lt;br /&gt;
dv = rc[:] # direct view&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Passing Things to Each Cluster ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; provides ways for the user to pass only the necessary items to the the clusters as to not overload them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Passing packages all together&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;with dv.sync_imports():&lt;br /&gt;
    from sklearn.linear_model import LinearRegression&lt;br /&gt;
    # getting dependencies on the clusters&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Passing variables:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Below is just an example - we don’t need to pass &amp;lt;code&amp;gt;my_k&amp;lt;/code&amp;gt; since &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; will directly get it when we want to use it.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;dv[&amp;quot;my_k&amp;quot;] = my_k&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;We can also use the &amp;lt;code&amp;gt;%px&amp;lt;/code&amp;gt; magick to run lines of code on all servers.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%px import numpy as np&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Actual Functionality ===&lt;br /&gt;
&lt;br /&gt;
The actual functionality is very similar to &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;maps&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;apply&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Synchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_sync(my_simulation, my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;201 ms ± 34.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Asynchronous ====&lt;br /&gt;
For asynchronous, you need to use &amp;lt;code&amp;gt;.get()&amp;lt;/code&amp;gt; in order to get the results at a later time/when they come in, since the work is not done in order and is instead chunked for efficiency.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_async(my_simulation, my_k).get() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;209 ms ± 28.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= Just in Time compilers + parallelization =&lt;br /&gt;
&lt;br /&gt;
TODO: &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;. The idea of these is to compile the function you will be using the first time you use it and then never compile it again (speeding up the actual use of the function). That is then combined with standard parallelization&lt;br /&gt;
= Additional Topics and Resources =&lt;br /&gt;
&lt;br /&gt;
# [https://ray.io/ &amp;lt;code&amp;gt;Ray&amp;lt;/code&amp;gt; python package] allows paralleilization across multiple computers&lt;br /&gt;
# [https://people.duke.edu/~ccc14/sta-663-2018/notebooks/S14A_Parallel_Programming_Introduction.html Duke’s statistical computing course] provides a set of examples for multiple python parallelization packages |&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=216</id>
		<title>Parallelization in python</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=216"/>
		<updated>2025-06-25T16:31:26Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: Updated the joblibs times to match what I am currently getting&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;This article is still being developed - released earlier to align with Department seminar.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In Python, parallelization is a bit more of a “wild west” in terms of the number of parallelization packages available. This wiki will attempt to present minor examples of some common approaches, along with an outline of things to think about in selecting the library you end up using. In general, the process in Python involves:&lt;br /&gt;
&lt;br /&gt;
# defining a function that captures what you want to have parallelized&lt;br /&gt;
# calling that function repeatedly in the the parallelization package way (which often looks like a list-recursive call).&lt;br /&gt;
&lt;br /&gt;
There are a few different general frameworks to accomplish this.&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;General wrappers&#039;&#039;&#039; define the cores you’ll be using and then wrap that around the function call in a list-recursive way.&lt;br /&gt;
# &#039;&#039;&#039;Just in time compilers&#039;&#039;&#039;, in addition to providing parallelization tools, provide decorators (e.g. &amp;lt;code&amp;gt;@jit&amp;lt;/code&amp;gt;) for functions that allow for just in time compilation of the Python function into lower level machine code (like &amp;lt;code&amp;gt;C++&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
We will also discuss Python packages that are built specifically to parallelize jobs inside &#039;&#039;&#039;Jupyter notebooks&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
= Non-parallelized: Single Core Example =&lt;br /&gt;
&lt;br /&gt;
We naturally want to compare a parallelization approach with the base single core analysis, which can be done with the code as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;#base for-loop&lt;br /&gt;
import numpy as np&lt;br /&gt;
from sklearn.linear_model import LinearRegression&lt;br /&gt;
&lt;br /&gt;
my_k = list(np.arange(14)+1)*20&lt;br /&gt;
&lt;br /&gt;
def my_simulation(k):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    estamine the polynomial fit of degree k&#039;s MSE for a linear&lt;br /&gt;
    relationship&lt;br /&gt;
&lt;br /&gt;
    Parameters&lt;br /&gt;
    ----------&lt;br /&gt;
    k : int&lt;br /&gt;
    degree of polynomial fit&lt;br /&gt;
&lt;br /&gt;
    Returns&lt;br /&gt;
    -------&lt;br /&gt;
    mse of fit based on 30 points applied to a test set of 10000 points&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    x = np.random.uniform(size=30,low=-3,high=3)&lt;br /&gt;
    y = 2*x + 2* np.random.normal(size=30)&lt;br /&gt;
    y = y.reshape(-1,1)&lt;br /&gt;
    X_poly = np.concatenate([x.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    x0 = np.random.uniform(size=10000,low=-3,high=3)&lt;br /&gt;
    y0 = 2*x0 + 2*np.random.normal(size=10000)&lt;br /&gt;
    y0 = y0.reshape(-1,1)&lt;br /&gt;
    X0_poly = np.concatenate([x0.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
    my_lm = LinearRegression().fit(X_poly,y)&lt;br /&gt;
    y0_hat = my_lm.predict(X0_poly)&lt;br /&gt;
&lt;br /&gt;
    return(np.mean((y0-y0_hat)**2))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import time &lt;br /&gt;
&lt;br /&gt;
starttime = time.time()&lt;br /&gt;
[my_simulation(k) for k in my_k]&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.9454693794250488 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= General Python Parallelization Packages =&lt;br /&gt;
&lt;br /&gt;
As previously mentioned, there are a lot of packages for general parallelization. Two commonly used ones are &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; looks a bit more like list-recursion and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; uses functions like &amp;lt;code&amp;gt;.map()&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; package is actually built into Python, and works in a pretty straightforward manner. It also allows for synchronous vs. asynchronous parallelization, where synchronous means the order of the jobs must be followed, one after the next, and asynchronous means multiple tasks can be executed in small parts in overlapping periods. Note that many statistical uses of parallelization do not need synchronous parallelization.&lt;br /&gt;
&lt;br /&gt;
Since both approaches have the same syntax we’ll be using &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; (as it works with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebooks&amp;lt;/code&amp;gt;). &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; can have problems with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s. In &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s, if you want to use &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; , it needs to be wrapped in &amp;lt;code&amp;gt;if name == &amp;quot;main&amp;quot;&amp;lt;/code&amp;gt; to properly fork processes; you can also save your process is as a .py file and import it into your &amp;lt;code&amp;gt;jupyter notebook.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
First, you need to identify the number of cores you have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import multiprocess as mp &lt;br /&gt;
#Could also using import multiprocessing as mp; the syntax is the same&lt;br /&gt;
&lt;br /&gt;
num_cores = mp.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Synchronous  ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;starttime = time.time()&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
  results = p.map(my_simulation, my_k)&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.7638578414916992 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Asynchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;starttime = time.time()&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
    results = p.map_async(my_simulation, my_k).get() &lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&lt;br /&gt;
    &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.8183681964874268 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
== &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; package is pretty straightforward and requires you to structure things in terms of list-recursion with a few wrappers to help the process work as desired. Just like with &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;, the number of cores needs to be computed before proceeding.&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import joblib&lt;br /&gt;
&lt;br /&gt;
num_cores = joblib.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;To use &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; functionality, we can import the functions &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; creates an instance for parallelization with a set number of jobs based on the core availability.  &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt; creates deferred function calls to distribute computations across multiple cores; this is the function that allows for simultaneous task execution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from joblib import Parallel, delayed&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
We then can move our list-recursive example into our &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; structure. We wrap &amp;lt;code&amp;gt;Parallel(...)()&amp;lt;/code&amp;gt; around the inside of the list-recursion and &amp;lt;code&amp;gt;delayed()&amp;lt;/code&amp;gt; around the functions that will be repeated multiple times.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;starttime = time.time()&lt;br /&gt;
r = Parallel(n_jobs=num_cores-2)(delayed(my_simulation)(k) for k in my_k)&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.5306367874145508 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Add-ons ===&lt;br /&gt;
&lt;br /&gt;
There are multiple add-ons that can be desirable&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;Verbosity&#039;&#039;&#039; can be added with a single parameter use (as seen below). This will provide more detailed output information.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;r = Parallel(n_jobs=num_cores-2, verbose=10)(delayed(my_simulation)(k) for k in my_k)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;Multiple parameters&#039;&#039;&#039; can be passed with redefinitions of functions to take groupings. To replace multiple for-loops, often the function &amp;lt;code&amp;gt;itertools.product()&amp;lt;/code&amp;gt; is used for the parameters.&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Jupyter Notebook: &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt;=&lt;br /&gt;
&lt;br /&gt;
For those that heavily use Jupyter notebooks, the package &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; works quite nicely. Some benefits of this package include its verbosity, its ability to serialize numpy arrays cleanly, and the fact that it can distribute across multiple machines. This package also works with &amp;lt;code&amp;gt;numba.jit&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Before opening your &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; you’ll want to make sure the extension is enabled - which can be done from the command line with the following command or inside the &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; extensions tab.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;ipcluster nbextension enable&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
You’ll then need to go to the “IPython Clusters” tab in &amp;lt;code&amp;gt;jupyter noteook&amp;lt;/code&amp;gt; and start some parallel clusters.&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; parallelization ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import ipyparallel as ipp&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
For direct execution we define &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; and provide it with what it needs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;rc = ipp.Client()&lt;br /&gt;
dv = rc[:] # direct view&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Passing Things to Each Cluster ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; provides ways for the user to pass only the necessary items to the the clusters as to not overload them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Passing packages all together&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;with dv.sync_imports():&lt;br /&gt;
    from sklearn.linear_model import LinearRegression&lt;br /&gt;
    # getting dependencies on the clusters&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Passing variables:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Below is just an example - we don’t need to pass &amp;lt;code&amp;gt;my_k&amp;lt;/code&amp;gt; since &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; will directly get it when we want to use it.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;dv[&amp;quot;my_k&amp;quot;] = my_k&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;We can also use the &amp;lt;code&amp;gt;%px&amp;lt;/code&amp;gt; magick to run lines of code on all servers.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%px import numpy as np&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Actual Functionality ===&lt;br /&gt;
&lt;br /&gt;
The actual functionality is very similar to &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;maps&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;apply&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Synchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_sync(my_simulation, my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;201 ms ± 34.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Asynchronous ====&lt;br /&gt;
For asynchronous, you need to use &amp;lt;code&amp;gt;.get()&amp;lt;/code&amp;gt; in order to get the results at a later time/when they come in, since the work is not done in order and is instead chunked for efficiency.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_async(my_simulation, my_k).get() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;209 ms ± 28.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= Just in Time compilers + parallelization =&lt;br /&gt;
&lt;br /&gt;
TODO: &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;. The idea of these is to compile the function you will be using the first time you use it and then never compile it again (speeding up the actual use of the function). That is then combined with standard parallelization&lt;br /&gt;
= Additional Topics and Resources =&lt;br /&gt;
&lt;br /&gt;
# [https://ray.io/ &amp;lt;code&amp;gt;Ray&amp;lt;/code&amp;gt; python package] allows paralleilization across multiple computers&lt;br /&gt;
# [https://people.duke.edu/~ccc14/sta-663-2018/notebooks/S14A_Parallel_Programming_Introduction.html Duke’s statistical computing course] provides a set of examples for multiple python parallelization packages |&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=215</id>
		<title>Parallelization in python</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=215"/>
		<updated>2025-06-25T16:24:59Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: Updated with the new times I&amp;#039;m getting so the results are consistent&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;This article is still being developed - released earlier to align with Department seminar.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In Python, parallelization is a bit more of a “wild west” in terms of the number of parallelization packages available. This wiki will attempt to present minor examples of some common approaches, along with an outline of things to think about in selecting the library you end up using. In general, the process in Python involves:&lt;br /&gt;
&lt;br /&gt;
# defining a function that captures what you want to have parallelized&lt;br /&gt;
# calling that function repeatedly in the the parallelization package way (which often looks like a list-recursive call).&lt;br /&gt;
&lt;br /&gt;
There are a few different general frameworks to accomplish this.&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;General wrappers&#039;&#039;&#039; define the cores you’ll be using and then wrap that around the function call in a list-recursive way.&lt;br /&gt;
# &#039;&#039;&#039;Just in time compilers&#039;&#039;&#039;, in addition to providing parallelization tools, provide decorators (e.g. &amp;lt;code&amp;gt;@jit&amp;lt;/code&amp;gt;) for functions that allow for just in time compilation of the Python function into lower level machine code (like &amp;lt;code&amp;gt;C++&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
We will also discuss Python packages that are built specifically to parallelize jobs inside &#039;&#039;&#039;Jupyter notebooks&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
= Non-parallelized: Single Core Example =&lt;br /&gt;
&lt;br /&gt;
We naturally want to compare a parallelization approach with the base single core analysis, which can be done with the code as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;#base for-loop&lt;br /&gt;
import numpy as np&lt;br /&gt;
from sklearn.linear_model import LinearRegression&lt;br /&gt;
&lt;br /&gt;
my_k = list(np.arange(14)+1)*20&lt;br /&gt;
&lt;br /&gt;
def my_simulation(k):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    estamine the polynomial fit of degree k&#039;s MSE for a linear&lt;br /&gt;
    relationship&lt;br /&gt;
&lt;br /&gt;
    Parameters&lt;br /&gt;
    ----------&lt;br /&gt;
    k : int&lt;br /&gt;
    degree of polynomial fit&lt;br /&gt;
&lt;br /&gt;
    Returns&lt;br /&gt;
    -------&lt;br /&gt;
    mse of fit based on 30 points applied to a test set of 10000 points&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    x = np.random.uniform(size=30,low=-3,high=3)&lt;br /&gt;
    y = 2*x + 2* np.random.normal(size=30)&lt;br /&gt;
    y = y.reshape(-1,1)&lt;br /&gt;
    X_poly = np.concatenate([x.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    x0 = np.random.uniform(size=10000,low=-3,high=3)&lt;br /&gt;
    y0 = 2*x0 + 2*np.random.normal(size=10000)&lt;br /&gt;
    y0 = y0.reshape(-1,1)&lt;br /&gt;
    X0_poly = np.concatenate([x0.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
    my_lm = LinearRegression().fit(X_poly,y)&lt;br /&gt;
    y0_hat = my_lm.predict(X0_poly)&lt;br /&gt;
&lt;br /&gt;
    return(np.mean((y0-y0_hat)**2))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import time &lt;br /&gt;
&lt;br /&gt;
starttime = time.time()&lt;br /&gt;
[my_simulation(k) for k in my_k]&lt;br /&gt;
print(str(time.time() - starttime) + &amp;quot; seconds&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;0.9454693794250488 seconds&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= General Python Parallelization Packages =&lt;br /&gt;
&lt;br /&gt;
As previously mentioned, there are a lot of packages for general parallelization. Two commonly used ones are &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; looks a bit more like list-recursion and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; uses functions like &amp;lt;code&amp;gt;.map()&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; package is actually built into Python, and works in a pretty straightforward manner. It also allows for synchronous vs. asynchronous parallelization, where synchronous means the order of the jobs must be followed, one after the next, and asynchronous means multiple tasks can be executed in small parts in overlapping periods. Note that many statistical uses of parallelization do not need synchronous parallelization.&lt;br /&gt;
&lt;br /&gt;
Since both approaches have the same syntax we’ll be using &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; (as it works with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebooks&amp;lt;/code&amp;gt;). &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; can have problems with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s. In &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s, if you want to use &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; , it needs to be wrapped in &amp;lt;code&amp;gt;if name == &amp;quot;main&amp;quot;&amp;lt;/code&amp;gt; to properly fork processes; you can also save your process is as a .py file and import it into your &amp;lt;code&amp;gt;jupyter notebook.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
First, you need to identify the number of cores you have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import multiprocess as mp &lt;br /&gt;
#Could also using import multiprocessing as mp; the syntax is the same&lt;br /&gt;
&lt;br /&gt;
num_cores = mp.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Synchronous  ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
  results = p.map(my_simulation, my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;209 ms ± 24.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Asynchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
    results = p.map_async(my_simulation, my_k).get() # not clear why I needed to use .get() to get something returnable for async&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;48.9 ms ± 3.08 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
== &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; package is pretty straightforward and requires you to structure things in terms of list-recursion with a few wrappers to help the process work as desired. Just like with &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;, the number of cores needs to be computed before proceeding.&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import joblib&lt;br /&gt;
&lt;br /&gt;
num_cores = joblib.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;To use &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; functionality, we can import the functions &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; creates an instance for parallelization with a set number of jobs based on the core availability.  &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt; creates deferred function calls to distribute computations across multiple cores; this is the function that allows for simultaneous task execution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from joblib import Parallel, delayed&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
We then can move our list-recursive example into our &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; structure. We wrap &amp;lt;code&amp;gt;Parallel(...)()&amp;lt;/code&amp;gt; around the inside of the list-recursion and &amp;lt;code&amp;gt;delayed()&amp;lt;/code&amp;gt; around the functions that will be repeated multiple times.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%% timeit&lt;br /&gt;
&lt;br /&gt;
r = Parallel(n_jobs=num_cores-2)(delayed(my_simulation)(k) for k in my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;172 ms ± 23.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Add-ons ===&lt;br /&gt;
&lt;br /&gt;
There are multiple add-ons that can be desirable&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;Verbosity&#039;&#039;&#039; can be added with a single parameter use (as seen below). This will provide more detailed output information.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;r = Parallel(n_jobs=num_cores-2, verbose=10)(delayed(my_simulation)(k) for k in my_k)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;Multiple parameters&#039;&#039;&#039; can be passed with redefinitions of functions to take groupings. To replace multiple for-loops, often the function &amp;lt;code&amp;gt;itertools.product()&amp;lt;/code&amp;gt; is used for the parameters.&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Jupyter Notebook: &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt;=&lt;br /&gt;
&lt;br /&gt;
For those that heavily use Jupyter notebooks, the package &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; works quite nicely. Some benefits of this package include its verbosity, its ability to serialize numpy arrays cleanly, and the fact that it can distribute across multiple machines. This package also works with &amp;lt;code&amp;gt;numba.jit&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Before opening your &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; you’ll want to make sure the extension is enabled - which can be done from the command line with the following command or inside the &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; extensions tab.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;ipcluster nbextension enable&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
You’ll then need to go to the “IPython Clusters” tab in &amp;lt;code&amp;gt;jupyter noteook&amp;lt;/code&amp;gt; and start some parallel clusters.&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; parallelization ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import ipyparallel as ipp&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
For direct execution we define &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; and provide it with what it needs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;rc = ipp.Client()&lt;br /&gt;
dv = rc[:] # direct view&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Passing Things to Each Cluster ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; provides ways for the user to pass only the necessary items to the the clusters as to not overload them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Passing packages all together&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;with dv.sync_imports():&lt;br /&gt;
    from sklearn.linear_model import LinearRegression&lt;br /&gt;
    # getting dependencies on the clusters&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Passing variables:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Below is just an example - we don’t need to pass &amp;lt;code&amp;gt;my_k&amp;lt;/code&amp;gt; since &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; will directly get it when we want to use it.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;dv[&amp;quot;my_k&amp;quot;] = my_k&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;We can also use the &amp;lt;code&amp;gt;%px&amp;lt;/code&amp;gt; magick to run lines of code on all servers.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%px import numpy as np&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Actual Functionality ===&lt;br /&gt;
&lt;br /&gt;
The actual functionality is very similar to &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;maps&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;apply&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Synchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_sync(my_simulation, my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;201 ms ± 34.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Asynchronous ====&lt;br /&gt;
For asynchronous, you need to use &amp;lt;code&amp;gt;.get()&amp;lt;/code&amp;gt; in order to get the results at a later time/when they come in, since the work is not done in order and is instead chunked for efficiency.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_async(my_simulation, my_k).get() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;209 ms ± 28.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= Just in Time compilers + parallelization =&lt;br /&gt;
&lt;br /&gt;
TODO: &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;. The idea of these is to compile the function you will be using the first time you use it and then never compile it again (speeding up the actual use of the function). That is then combined with standard parallelization&lt;br /&gt;
= Additional Topics and Resources =&lt;br /&gt;
&lt;br /&gt;
# [https://ray.io/ &amp;lt;code&amp;gt;Ray&amp;lt;/code&amp;gt; python package] allows paralleilization across multiple computers&lt;br /&gt;
# [https://people.duke.edu/~ccc14/sta-663-2018/notebooks/S14A_Parallel_Programming_Introduction.html Duke’s statistical computing course] provides a set of examples for multiple python parallelization packages |&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=214</id>
		<title>Parallelization in python</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=214"/>
		<updated>2025-06-25T16:00:00Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;This article is still being developed - released earlier to align with Department seminar.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In Python, parallelization is a bit more of a “wild west” in terms of the number of parallelization packages available. This wiki will attempt to present minor examples of some common approaches, along with an outline of things to think about in selecting the library you end up using. In general, the process in Python involves:&lt;br /&gt;
&lt;br /&gt;
# defining a function that captures what you want to have parallelized&lt;br /&gt;
# calling that function repeatedly in the the parallelization package way (which often looks like a list-recursive call).&lt;br /&gt;
&lt;br /&gt;
There are a few different general frameworks to accomplish this.&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;General wrappers&#039;&#039;&#039; define the cores you’ll be using and then wrap that around the function call in a list-recursive way.&lt;br /&gt;
# &#039;&#039;&#039;Just in time compilers&#039;&#039;&#039;, in addition to providing parallelization tools, provide decorators (e.g. &amp;lt;code&amp;gt;@jit&amp;lt;/code&amp;gt;) for functions that allow for just in time compilation of the Python function into lower level machine code (like &amp;lt;code&amp;gt;C++&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
We will also discuss Python packages that are built specifically to parallelize jobs inside &#039;&#039;&#039;Jupyter notebooks&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
= Non-parallelized: Single Core Example =&lt;br /&gt;
&lt;br /&gt;
We naturally want to compare a parallelization approach with the base single core analysis, which can be done with the code as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;#base for-loop&lt;br /&gt;
import numpy as np&lt;br /&gt;
from sklearn.linear_model import LinearRegression&lt;br /&gt;
&lt;br /&gt;
my_k = list(np.arange(14)+1)*20&lt;br /&gt;
&lt;br /&gt;
def my_simulation(k):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    estamine the polynomial fit of degree k&#039;s MSE for a linear&lt;br /&gt;
    relationship&lt;br /&gt;
&lt;br /&gt;
    Parameters&lt;br /&gt;
    ----------&lt;br /&gt;
    k : int&lt;br /&gt;
    degree of polynomial fit&lt;br /&gt;
&lt;br /&gt;
    Returns&lt;br /&gt;
    -------&lt;br /&gt;
    mse of fit based on 30 points applied to a test set of 10000 points&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    x = np.random.uniform(size=30,low=-3,high=3)&lt;br /&gt;
    y = 2*x + 2* np.random.normal(size=30)&lt;br /&gt;
    y = y.reshape(-1,1)&lt;br /&gt;
    X_poly = np.concatenate([x.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    x0 = np.random.uniform(size=10000,low=-3,high=3)&lt;br /&gt;
    y0 = 2*x0 + 2*np.random.normal(size=10000)&lt;br /&gt;
    y0 = y0.reshape(-1,1)&lt;br /&gt;
    X0_poly = np.concatenate([x0.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
    my_lm = LinearRegression().fit(X_poly,y)&lt;br /&gt;
    y0_hat = my_lm.predict(X0_poly)&lt;br /&gt;
&lt;br /&gt;
    return(np.mean((y0-y0_hat)**2))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit -n3&lt;br /&gt;
&lt;br /&gt;
[my_simulation(k) for k in my_k]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;676 ms ± 37.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= General Python Parallelization Packages =&lt;br /&gt;
&lt;br /&gt;
As previously mentioned, there are a lot of packages for general parallelization. Two commonly used ones are &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; looks a bit more like list-recursion and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; uses functions like &amp;lt;code&amp;gt;.map()&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; package is actually built into Python, and works in a pretty straightforward manner. It also allows for synchronous vs. asynchronous parallelization, where synchronous means the order of the jobs must be followed, one after the next, and asynchronous means multiple tasks can be executed in small parts in overlapping periods. Note that many statistical uses of parallelization do not need synchronous parallelization.&lt;br /&gt;
&lt;br /&gt;
Since both approaches have the same syntax we’ll be using &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; (as it works with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebooks&amp;lt;/code&amp;gt;). &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; can have problems with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s. In &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s, if you want to use &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; , it needs to be wrapped in &amp;lt;code&amp;gt;if name == &amp;quot;main&amp;quot;&amp;lt;/code&amp;gt; to properly fork processes; you can also save your process is as a .py file and import it into your &amp;lt;code&amp;gt;jupyter notebook.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
First, you need to identify the number of cores you have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import multiprocess as mp &lt;br /&gt;
#Could also using import multiprocessing as mp; the syntax is the same&lt;br /&gt;
&lt;br /&gt;
num_cores = mp.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Synchronous  ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
  results = p.map(my_simulation, my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;209 ms ± 24.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Asynchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
    results = p.map_async(my_simulation, my_k).get() # not clear why I needed to use .get() to get something returnable for async&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;48.9 ms ± 3.08 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
== &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; package is pretty straightforward and requires you to structure things in terms of list-recursion with a few wrappers to help the process work as desired. Just like with &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;, the number of cores needs to be computed before proceeding.&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import joblib&lt;br /&gt;
&lt;br /&gt;
num_cores = joblib.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;To use &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; functionality, we can import the functions &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; creates an instance for parallelization with a set number of jobs based on the core availability.  &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt; creates deferred function calls to distribute computations across multiple cores; this is the function that allows for simultaneous task execution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from joblib import Parallel, delayed&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
We then can move our list-recursive example into our &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; structure. We wrap &amp;lt;code&amp;gt;Parallel(...)()&amp;lt;/code&amp;gt; around the inside of the list-recursion and &amp;lt;code&amp;gt;delayed()&amp;lt;/code&amp;gt; around the functions that will be repeated multiple times.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%% timeit&lt;br /&gt;
&lt;br /&gt;
r = Parallel(n_jobs=num_cores-2)(delayed(my_simulation)(k) for k in my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;172 ms ± 23.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Add-ons ===&lt;br /&gt;
&lt;br /&gt;
There are multiple add-ons that can be desirable&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;Verbosity&#039;&#039;&#039; can be added with a single parameter use (as seen below). This will provide more detailed output information.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;r = Parallel(n_jobs=num_cores-2, verbose=10)(delayed(my_simulation)(k) for k in my_k)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;Multiple parameters&#039;&#039;&#039; can be passed with redefinitions of functions to take groupings. To replace multiple for-loops, often the function &amp;lt;code&amp;gt;itertools.product()&amp;lt;/code&amp;gt; is used for the parameters.&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Jupyter Notebook: &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt;=&lt;br /&gt;
&lt;br /&gt;
For those that heavily use Jupyter notebooks, the package &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; works quite nicely. Some benefits of this package include its verbosity, its ability to serialize numpy arrays cleanly, and the fact that it can distribute across multiple machines. This package also works with &amp;lt;code&amp;gt;numba.jit&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Before opening your &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; you’ll want to make sure the extension is enabled - which can be done from the command line with the following command or inside the &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; extensions tab.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;ipcluster nbextension enable&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
You’ll then need to go to the “IPython Clusters” tab in &amp;lt;code&amp;gt;jupyter noteook&amp;lt;/code&amp;gt; and start some parallel clusters.&lt;br /&gt;
&lt;br /&gt;
==&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; parallelization ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import ipyparallel as ipp&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
For direct execution we define &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; and provide it with what it needs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;rc = ipp.Client()&lt;br /&gt;
dv = rc[:] # direct view&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Passing Things to Each Cluster ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; provides ways for the user to pass only the necessary items to the the clusters as to not overload them.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Passing packages all together&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;with dv.sync_imports():&lt;br /&gt;
    from sklearn.linear_model import LinearRegression&lt;br /&gt;
    # getting dependencies on the clusters&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;Passing variables:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Below is just an example - we don’t need to pass &amp;lt;code&amp;gt;my_k&amp;lt;/code&amp;gt; since &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; will directly get it when we want to use it.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;dv[&amp;quot;my_k&amp;quot;] = my_k&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;We can also use the &amp;lt;code&amp;gt;%px&amp;lt;/code&amp;gt; magick to run lines of code on all servers.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%px import numpy as np&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Actual Functionality ===&lt;br /&gt;
&lt;br /&gt;
The actual functionality is very similar to &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;maps&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;apply&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Synchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_sync(my_simulation, my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;201 ms ± 34.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Asynchronous ====&lt;br /&gt;
For asynchronous, you need to use &amp;lt;code&amp;gt;.get()&amp;lt;/code&amp;gt; in order to get the results at a later time/when they come in, since the work is not done in order and is instead chunked for efficiency.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_async(my_simulation, my_k).get() &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;209 ms ± 28.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= Just in Time compilers + parallelization =&lt;br /&gt;
&lt;br /&gt;
TODO: &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;. The idea of these is to compile the function you will be using the first time you use it and then never compile it again (speeding up the actual use of the function). That is then combined with standard parallelization&lt;br /&gt;
= Additional Topics and Resources =&lt;br /&gt;
&lt;br /&gt;
# [https://ray.io/ &amp;lt;code&amp;gt;Ray&amp;lt;/code&amp;gt; python package] allows paralleilization across multiple computers&lt;br /&gt;
# [https://people.duke.edu/~ccc14/sta-663-2018/notebooks/S14A_Parallel_Programming_Introduction.html Duke’s statistical computing course] provides a set of examples for multiple python parallelization packages |&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=213</id>
		<title>Parallelization in python</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=213"/>
		<updated>2025-06-25T14:49:05Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: Cleaned up and added further explanation to multiprocessing and joblib&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;This article is still being developed - released earlier to align with Department seminar.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In Python, parallelization is a bit more of a “wild west” in terms of the number of parallelization packages available. This wiki will attempt to present minor examples of some common approaches, along with an outline of things to think about in selecting the library you end up using. In general, the process in Python involves:&lt;br /&gt;
&lt;br /&gt;
# defining a function that captures what you want to have parallelized&lt;br /&gt;
# calling that function repeatedly in the the parallelization package way (which often looks like a list-recursive call).&lt;br /&gt;
&lt;br /&gt;
There are a few different general frameworks to accomplish this.&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;General wrappers&#039;&#039;&#039; define the cores you’ll be using and then wrap that around the function call in a list-recursive way.&lt;br /&gt;
# &#039;&#039;&#039;Just in time compilers&#039;&#039;&#039;, in addition to providing parallelization tools, provide decorators (e.g. &amp;lt;code&amp;gt;@jit&amp;lt;/code&amp;gt;) for functions that allow for just in time compilation of the Python function into lower level machine code (like &amp;lt;code&amp;gt;C++&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
We will also discuss Python packages that are built specifically to parallelize jobs inside &#039;&#039;&#039;Jupyter notebooks&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
= Non-parallelized: Single Core Example =&lt;br /&gt;
&lt;br /&gt;
We naturally want to compare a parallelization approach with the base single core analysis, which can be done with the code as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;#base for-loop&lt;br /&gt;
import numpy as np&lt;br /&gt;
from sklearn.linear_model import LinearRegression&lt;br /&gt;
&lt;br /&gt;
my_k = list(np.arange(14)+1)*20&lt;br /&gt;
&lt;br /&gt;
def my_simulation(k):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    estamine the polynomial fit of degree k&#039;s MSE for a linear&lt;br /&gt;
    relationship&lt;br /&gt;
&lt;br /&gt;
    Parameters&lt;br /&gt;
    ----------&lt;br /&gt;
    k : int&lt;br /&gt;
    degree of polynomial fit&lt;br /&gt;
&lt;br /&gt;
    Returns&lt;br /&gt;
    -------&lt;br /&gt;
    mse of fit based on 30 points applied to a test set of 10000 points&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    x = np.random.uniform(size=30,low=-3,high=3)&lt;br /&gt;
    y = 2*x + 2* np.random.normal(size=30)&lt;br /&gt;
    y = y.reshape(-1,1)&lt;br /&gt;
    X_poly = np.concatenate([x.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    x0 = np.random.uniform(size=10000,low=-3,high=3)&lt;br /&gt;
    y0 = 2*x0 + 2*np.random.normal(size=10000)&lt;br /&gt;
    y0 = y0.reshape(-1,1)&lt;br /&gt;
    X0_poly = np.concatenate([x0.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
    my_lm = LinearRegression().fit(X_poly,y)&lt;br /&gt;
    y0_hat = my_lm.predict(X0_poly)&lt;br /&gt;
&lt;br /&gt;
    return(np.mean((y0-y0_hat)**2))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit -n3&lt;br /&gt;
&lt;br /&gt;
[my_simulation(k) for k in my_k]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;676 ms ± 37.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= General Python Parallelization Packages =&lt;br /&gt;
&lt;br /&gt;
As previously mentioned, there are a lot of packages for general parallelization. Two commonly used ones are &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; looks a bit more like list-recursion and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; uses functions like &amp;lt;code&amp;gt;.map()&amp;lt;/code&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; package is actually built into Python, and works in a pretty straightforward manner. It also allows for synchronous vs. asynchronous parallelization, where synchronous means the order of the jobs must be followed, one after the next, and asynchronous means multiple tasks can be executed in small parts in overlapping periods. Note that many statistical uses of parallelization do not need synchronous parallelization.&lt;br /&gt;
&lt;br /&gt;
Since both approaches have the same syntax we’ll be using &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; (as it works with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebooks&amp;lt;/code&amp;gt;). &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; can have problems with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s. In &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s, if you want to use &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; , it needs to be wrapped in &amp;lt;code&amp;gt;if name == &amp;quot;main&amp;quot;&amp;lt;/code&amp;gt; to properly fork processes; you can also save your process is as a .py file and import it into your &amp;lt;code&amp;gt;jupyter notebook.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
First, you need to identify the number of cores you have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import multiprocess as mp &lt;br /&gt;
#Could also using import multiprocessing as mp; the syntax is the same&lt;br /&gt;
&lt;br /&gt;
num_cores = mp.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Synchronous  ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
  results = p.map(my_simulation, my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;209 ms ± 24.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== Asynchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
    results = p.map_async(my_simulation, my_k).get() # not clear why I needed to use .get() to get something returnable for async&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;48.9 ms ± 3.08 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
== &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; package is pretty straightforward and requires you to structure things in terms of list-recursion with a few wrappers to help the process work as desired. Just like with &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;, the number of cores needs to be computed before proceeding.&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import joblib&lt;br /&gt;
&lt;br /&gt;
num_cores = joblib.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;To use &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; functionality, we can import the functions &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; creates an instance for parallelization with a set number of jobs based on the core availability.  &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt; creates deferred function calls to distribute computations across multiple cores; this is the function that allows for simultaneous task execution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from joblib import Parallel, delayed&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
We then can move our list-recursive example into our &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; structure. We wrap &amp;lt;code&amp;gt;Parallel(...)()&amp;lt;/code&amp;gt; around the inside of the list-recursion and &amp;lt;code&amp;gt;delayed()&amp;lt;/code&amp;gt; around the functions that will be repeated multiple times.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%% timeit&lt;br /&gt;
&lt;br /&gt;
r = Parallel(n_jobs=num_cores-2)(delayed(my_simulation)(k) for k in my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;172 ms ± 23.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Add-ons ===&lt;br /&gt;
&lt;br /&gt;
There are multiple add-ons that can be desirable&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;Verbosity&#039;&#039;&#039; can be added with a single parameter use (as seen below). This will provide more detailed output information.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;r = Parallel(n_jobs=num_cores-2, verbose=10)(delayed(my_simulation)(k) for k in my_k)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;Multiple parameters&#039;&#039;&#039; can be passed with redefinitions of functions to take groupings. To replace multiple for-loops, often the function &amp;lt;code&amp;gt;itertools.product()&amp;lt;/code&amp;gt; is used for the parameters.&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Just in Time compilers + parallelization =&lt;br /&gt;
&lt;br /&gt;
TODO: &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;. The idea of these is to compile the function you will be using the first time you use it and then never compile it again (speeding up the actual use of the function). That is then combined with standard parallelization&lt;br /&gt;
&lt;br /&gt;
= Jupyter notebook: &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; =&lt;br /&gt;
&lt;br /&gt;
For those that heavily use jupyter notebooks and nothing else (which this author personally doesn’t encourage), the package &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; nicely works inside jupyter notebooks (but so does many of the other packages above like &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;, etc.). One nice this about this package is the verbosity, etc. works intellegently in the notebook. This package also works with &amp;lt;code&amp;gt;numba.jit&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Before opening your &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; you’ll want to make sure the extension is enabled - which can be down from the command line with the following command or inside the &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; extensions tab.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;ipcluster nbextension enable&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
And then you’ll need to go to the “IPython Clusters” tab in &amp;lt;code&amp;gt;jupyter noteook&amp;lt;/code&amp;gt; and start some parallel clusters.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; parallelization ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import ipyparallel as ipp&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
For direct execution we define &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; and provide it with what it needs…&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;rc = ipp.Client()&lt;br /&gt;
dv = rc[:] # direct view&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== passing things to each cluster ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; provides ways for the user to pass only the necessary items to the the clusters (as to not overload the clusters with too many things).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;passing packages all together&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;with dv.sync_imports():&lt;br /&gt;
    from sklearn.linear_model import LinearRegression&lt;br /&gt;
    # getting dependencies on the clusters&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;passing variable over:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Below is just an example - we don’t need to pass &amp;lt;code&amp;gt;my_k&amp;lt;/code&amp;gt; since &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; will directly get it when we wnat to use it.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;dv[&amp;quot;my_k&amp;quot;] = my_k&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;we can also use the &amp;lt;code&amp;gt;%px&amp;lt;/code&amp;gt; magick to run lines of code on all servers. Given I think &amp;lt;code&amp;gt;numpy&amp;lt;/code&amp;gt; is cooler as &amp;lt;code&amp;gt;np&amp;lt;/code&amp;gt; I actually have to do it this way, not the &amp;lt;code&amp;gt;dv.sync_imports&amp;lt;/code&amp;gt; way.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%px import numpy as np&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== actual functionality ===&lt;br /&gt;
&lt;br /&gt;
The actual functionality is very similar to &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;maps&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;apply&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== synchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_sync(my_simulation, my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;201 ms ± 34.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== asynchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_async(my_simulation, my_k).get() # not clear why I needed to use .get() to get something returnable for async&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;209 ms ± 28.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= additional topics and resources =&lt;br /&gt;
&lt;br /&gt;
# [https://ray.io/ &amp;lt;code&amp;gt;Ray&amp;lt;/code&amp;gt; python package] allows paralleilization across multiple computers&lt;br /&gt;
# [https://people.duke.edu/~ccc14/sta-663-2018/notebooks/S14A_Parallel_Programming_Introduction.html Duke’s statistical computing course] provides a set of examples for multiple python parallelization packages |&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=212</id>
		<title>Parallelization in python</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=212"/>
		<updated>2025-06-25T14:30:32Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;This article is still being developed - released earlier to align with Department seminar.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In Python, parallelization is a bit more of a “wild west” in terms of the number of parallelization packages available. This wiki will attempt to present minor examples of some common approaches, along with an outline of things to think about in selecting the library you end up using. In general, the process in Python involves:&lt;br /&gt;
&lt;br /&gt;
# defining a function that captures what you want to have parallelized&lt;br /&gt;
# calling that function repeatedly in the the parallelization package way (which often looks like a list-recursive call).&lt;br /&gt;
&lt;br /&gt;
There are a few different general frameworks to accomplish this.&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;General wrappers&#039;&#039;&#039; define the cores you’ll be using and then wrap that around the function call in a list-recursive way.&lt;br /&gt;
# &#039;&#039;&#039;Just in time compilers&#039;&#039;&#039;, in addition to providing parallelization tools, provide decorators (e.g. &amp;lt;code&amp;gt;@jit&amp;lt;/code&amp;gt;) for functions that allow for just in time compilation of the Python function into lower level machine code (like &amp;lt;code&amp;gt;C++&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
We will also discuss Python packages that are built specifically to parallelize jobs inside &#039;&#039;&#039;Jupyter notebooks&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
= Non-parallelized: single core example =&lt;br /&gt;
&lt;br /&gt;
We naturally want to compare a parallelization approach with the base single core analysis, which can be done with the code as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;#base for-loop&lt;br /&gt;
import numpy as np&lt;br /&gt;
from sklearn.linear_model import LinearRegression&lt;br /&gt;
&lt;br /&gt;
my_k = list(np.arange(14)+1)*20&lt;br /&gt;
&lt;br /&gt;
def my_simulation(k):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    estamine the polynomial fit of degree k&#039;s MSE for a linear&lt;br /&gt;
    relationship&lt;br /&gt;
&lt;br /&gt;
    Parameters&lt;br /&gt;
    ----------&lt;br /&gt;
    k : int&lt;br /&gt;
    degree of polynomial fit&lt;br /&gt;
&lt;br /&gt;
    Returns&lt;br /&gt;
    -------&lt;br /&gt;
    mse of fit based on 30 points applied to a test set of 10000 points&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    x = np.random.uniform(size=30,low=-3,high=3)&lt;br /&gt;
    y = 2*x + 2* np.random.normal(size=30)&lt;br /&gt;
    y = y.reshape(-1,1)&lt;br /&gt;
    X_poly = np.concatenate([x.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    x0 = np.random.uniform(size=10000,low=-3,high=3)&lt;br /&gt;
    y0 = 2*x0 + 2*np.random.normal(size=10000)&lt;br /&gt;
    y0 = y0.reshape(-1,1)&lt;br /&gt;
    X0_poly = np.concatenate([x0.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
    my_lm = LinearRegression().fit(X_poly,y)&lt;br /&gt;
    y0_hat = my_lm.predict(X0_poly)&lt;br /&gt;
&lt;br /&gt;
    return(np.mean((y0-y0_hat)**2))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit -n3&lt;br /&gt;
&lt;br /&gt;
[my_simulation(k) for k in my_k]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;676 ms ± 37.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= General python parallelization packages =&lt;br /&gt;
&lt;br /&gt;
As mentioned there are a lot of package to do general parallelization. Two commonly used ones are &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; looks a bit more like list-recursion and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; uses functions like &amp;lt;code&amp;gt;.map()&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; can have problems with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s (though &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; wraps it and &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; is just a different fork of &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; package is actually built into python, and works in a pretty straightforward manner. It also allows for synchronous vs asynchronous parallelization, where synchronous means the order of the jobs must be followed and asynchronous where the order of jobs need not be followed. Many statistical uses of parallelization does not need synchronous parallelization.&lt;br /&gt;
&lt;br /&gt;
There since both approaches have the same syntax we’ll be using &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; (as it works with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebooks&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
=== parallelization ===&lt;br /&gt;
&lt;br /&gt;
First, you need to identify the number of cores you have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import multiprocess as mp&lt;br /&gt;
#import multiprocessing as mp&lt;br /&gt;
&lt;br /&gt;
num_cores = mp.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== synchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
  results = p.map(my_simulation, my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;209 ms ± 24.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== asynchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
    results = p.map_async(my_simulation, my_k).get() # not clear why I needed to use .get() to get something returnable for async&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;48.9 ms ± 3.08 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
== &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; package is pretty straightforward and requires you to structure things in terms of list-recursion with a few wrappers to help the process work as desired.&lt;br /&gt;
&lt;br /&gt;
=== setup ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import joblib&lt;br /&gt;
&lt;br /&gt;
num_cores = joblib.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== implimentation ===&lt;br /&gt;
&lt;br /&gt;
To use &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; functionality we can import the functions &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from joblib import Parallel, delayed&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
We then can move our list-recursive example into our &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; structure. We wrap &amp;lt;code&amp;gt;Parallel(...)()&amp;lt;/code&amp;gt; around the old of the inside of the list-recursion and &amp;lt;code&amp;gt;delayed()&amp;lt;/code&amp;gt; around the functions that will be repeated multiple times.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%% timeit&lt;br /&gt;
&lt;br /&gt;
r = Parallel(n_jobs=num_cores-2)(delayed(my_simulation)(k) for k in my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;172 ms ± 23.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Add-ons ===&lt;br /&gt;
&lt;br /&gt;
There are multiple add-ons that can be desirable&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;verbosity&#039;&#039;&#039; can be added with a single parameter use (as seen below) - the large the value, the more messages:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;r = Parallel(n_jobs=num_cores-2, verbose=10)(delayed(my_simulation)(k) for k in my_k)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;multiple parameters&#039;&#039;&#039; can be passed with redefinitions of functions to take groupings. To replace multiple for-loops often the function &amp;lt;code&amp;gt;itertools.product()&amp;lt;/code&amp;gt; is used for the parameters.&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Just in Time compilers + parallelization =&lt;br /&gt;
&lt;br /&gt;
TODO: &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;. The idea of these is to compile the function you will be using the first time you use it and then never compile it again (speeding up the actual use of the function). That is then combined with standard parallelization&lt;br /&gt;
&lt;br /&gt;
= Jupyter notebook: &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; =&lt;br /&gt;
&lt;br /&gt;
For those that heavily use jupyter notebooks and nothing else (which this author personally doesn’t encourage), the package &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; nicely works inside jupyter notebooks (but so does many of the other packages above like &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;, etc.). One nice this about this package is the verbosity, etc. works intellegently in the notebook. This package also works with &amp;lt;code&amp;gt;numba.jit&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Before opening your &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; you’ll want to make sure the extension is enabled - which can be down from the command line with the following command or inside the &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; extensions tab.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;ipcluster nbextension enable&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
And then you’ll need to go to the “IPython Clusters” tab in &amp;lt;code&amp;gt;jupyter noteook&amp;lt;/code&amp;gt; and start some parallel clusters.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; parallelization ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import ipyparallel as ipp&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
For direct execution we define &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; and provide it with what it needs…&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;rc = ipp.Client()&lt;br /&gt;
dv = rc[:] # direct view&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== passing things to each cluster ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; provides ways for the user to pass only the necessary items to the the clusters (as to not overload the clusters with too many things).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;passing packages all together&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;with dv.sync_imports():&lt;br /&gt;
    from sklearn.linear_model import LinearRegression&lt;br /&gt;
    # getting dependencies on the clusters&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;passing variable over:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Below is just an example - we don’t need to pass &amp;lt;code&amp;gt;my_k&amp;lt;/code&amp;gt; since &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; will directly get it when we wnat to use it.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;dv[&amp;quot;my_k&amp;quot;] = my_k&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;we can also use the &amp;lt;code&amp;gt;%px&amp;lt;/code&amp;gt; magick to run lines of code on all servers. Given I think &amp;lt;code&amp;gt;numpy&amp;lt;/code&amp;gt; is cooler as &amp;lt;code&amp;gt;np&amp;lt;/code&amp;gt; I actually have to do it this way, not the &amp;lt;code&amp;gt;dv.sync_imports&amp;lt;/code&amp;gt; way.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%px import numpy as np&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== actual functionality ===&lt;br /&gt;
&lt;br /&gt;
The actual functionality is very similar to &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;maps&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;apply&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== synchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_sync(my_simulation, my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;201 ms ± 34.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== asynchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_async(my_simulation, my_k).get() # not clear why I needed to use .get() to get something returnable for async&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;209 ms ± 28.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= additional topics and resources =&lt;br /&gt;
&lt;br /&gt;
# [https://ray.io/ &amp;lt;code&amp;gt;Ray&amp;lt;/code&amp;gt; python package] allows paralleilization across multiple computers&lt;br /&gt;
# [https://people.duke.edu/~ccc14/sta-663-2018/notebooks/S14A_Parallel_Programming_Introduction.html Duke’s statistical computing course] provides a set of examples for multiple python parallelization packages |&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
	<entry>
		<id>https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=211</id>
		<title>Parallelization in python</title>
		<link rel="alternate" type="text/html" href="https://info.stat.cmu.edu/index.php?title=Parallelization_in_python&amp;diff=211"/>
		<updated>2025-06-24T15:35:40Z</updated>

		<summary type="html">&lt;p&gt;Ecucuzze: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;This article is still being developed - released earlier to align with Department seminar.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In python, parallelization is a bit more of a “wild west” in terms of the number of packages that can help you parallelize your processes (This wiki will attempt to present minor examples of some along with an outline of things to think about in selecting the library you end up using. In general, the process in python involves&lt;br /&gt;
&lt;br /&gt;
# defining a function that captures what you want to have parallelized&lt;br /&gt;
# call that function repeatedly in the the parallelization package way (which often looks like a list-recursive call).&lt;br /&gt;
&lt;br /&gt;
There are a few different general frameworks to accomplish this.&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;general wrappers&#039;&#039;&#039;, define the cores you’ll be using and then wrap that around the function call in a list-recursive way&lt;br /&gt;
# &#039;&#039;&#039;just in time compilers&#039;&#039;&#039;, in addition to providing parallelization tools these packages provide decorators (e.g. &amp;lt;code&amp;gt;@jit&amp;lt;/code&amp;gt;) for functions that allow for just in time compilation of the python function into lower level machine code (like &amp;lt;code&amp;gt;C++&amp;lt;/code&amp;gt;)&lt;br /&gt;
&lt;br /&gt;
We will discuss a python package that is build specifically to parallelize jobs inside &#039;&#039;&#039;jupyter notebooks&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
= Non-parallelized: single core example =&lt;br /&gt;
&lt;br /&gt;
We naturally want to compare a parallelization approach with the base single core analysis, which can be done with the code as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;#base for-loop&lt;br /&gt;
import numpy as np&lt;br /&gt;
from sklearn.linear_model import LinearRegression&lt;br /&gt;
&lt;br /&gt;
my_k = list(np.arange(14)+1)*20&lt;br /&gt;
&lt;br /&gt;
def my_simulation(k):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    estamine the polynomial fit of degree k&#039;s MSE for a linear&lt;br /&gt;
    relationship&lt;br /&gt;
&lt;br /&gt;
    Parameters&lt;br /&gt;
    ----------&lt;br /&gt;
    k : int&lt;br /&gt;
    degree of polynomial fit&lt;br /&gt;
&lt;br /&gt;
    Returns&lt;br /&gt;
    -------&lt;br /&gt;
    mse of fit based on 30 points applied to a test set of 10000 points&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    x = np.random.uniform(size=30,low=-3,high=3)&lt;br /&gt;
    y = 2*x + 2* np.random.normal(size=30)&lt;br /&gt;
    y = y.reshape(-1,1)&lt;br /&gt;
    X_poly = np.concatenate([x.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    x0 = np.random.uniform(size=10000,low=-3,high=3)&lt;br /&gt;
    y0 = 2*x0 + 2*np.random.normal(size=10000)&lt;br /&gt;
    y0 = y0.reshape(-1,1)&lt;br /&gt;
    X0_poly = np.concatenate([x0.reshape(-1,1)**kk&lt;br /&gt;
                          for kk in np.arange(1,k+1,dtype = int)],&lt;br /&gt;
                          axis = 1)&lt;br /&gt;
&lt;br /&gt;
    my_lm = LinearRegression().fit(X_poly,y)&lt;br /&gt;
    y0_hat = my_lm.predict(X0_poly)&lt;br /&gt;
&lt;br /&gt;
    return(np.mean((y0-y0_hat)**2))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit -n3&lt;br /&gt;
&lt;br /&gt;
[my_simulation(k) for k in my_k]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;676 ms ± 37.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= General python parallelization packages =&lt;br /&gt;
&lt;br /&gt;
As mentioned there are a lot of package to do general parallelization. Two commonly used ones are &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; looks a bit more like list-recursion and &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; uses functions like &amp;lt;code&amp;gt;.map()&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; can have problems with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt;s (though &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; wraps it and &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; is just a different fork of &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; package is actually built into python, and works in a pretty straightforward manner. It also allows for synchronous vs asynchronous parallelization, where synchronous means the order of the jobs must be followed and asynchronous where the order of jobs need not be followed. Many statistical uses of parallelization does not need synchronous parallelization.&lt;br /&gt;
&lt;br /&gt;
There since both approaches have the same syntax we’ll be using &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt; (as it works with &amp;lt;code&amp;gt;ipython&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jupyter notebooks&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
=== parallelization ===&lt;br /&gt;
&lt;br /&gt;
First, you need to identify the number of cores you have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import multiprocess as mp&lt;br /&gt;
#import multiprocessing as mp&lt;br /&gt;
&lt;br /&gt;
num_cores = mp.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== synchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
  results = p.map(my_simulation, my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;209 ms ± 24.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== asynchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
&lt;br /&gt;
with mp.Pool(processes=num_cores-2) as p:&lt;br /&gt;
    results = p.map_async(my_simulation, my_k).get() # not clear why I needed to use .get() to get something returnable for async&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;48.9 ms ± 3.08 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
== &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; package is pretty straightforward and requires you to structure things in terms of list-recursion with a few wrappers to help the process work as desired.&lt;br /&gt;
&lt;br /&gt;
=== setup ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import joblib&lt;br /&gt;
&lt;br /&gt;
num_cores = joblib.cpu_count()&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== implimentation ===&lt;br /&gt;
&lt;br /&gt;
To use &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; functionality we can import the functions &amp;lt;code&amp;gt;Parallel&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;delayed&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;from joblib import Parallel, delayed&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
We then can move our list-recursive example into our &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt; structure. We wrap &amp;lt;code&amp;gt;Parallel(...)()&amp;lt;/code&amp;gt; around the old of the inside of the list-recursion and &amp;lt;code&amp;gt;delayed()&amp;lt;/code&amp;gt; around the functions that will be repeated multiple times.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%% timeit&lt;br /&gt;
&lt;br /&gt;
r = Parallel(n_jobs=num_cores-2)(delayed(my_simulation)(k) for k in my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;172 ms ± 23.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== Add-ons ===&lt;br /&gt;
&lt;br /&gt;
There are multiple add-ons that can be desirable&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;verbosity&#039;&#039;&#039; can be added with a single parameter use (as seen below) - the large the value, the more messages:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;r = Parallel(n_jobs=num_cores-2, verbose=10)(delayed(my_simulation)(k) for k in my_k)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;&#039;&#039;&#039;multiple parameters&#039;&#039;&#039; can be passed with redefinitions of functions to take groupings. To replace multiple for-loops often the function &amp;lt;code&amp;gt;itertools.product()&amp;lt;/code&amp;gt; is used for the parameters.&amp;lt;/p&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Just in Time compilers + parallelization =&lt;br /&gt;
&lt;br /&gt;
TODO: &amp;lt;code&amp;gt;numba&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;. The idea of these is to compile the function you will be using the first time you use it and then never compile it again (speeding up the actual use of the function). That is then combined with standard parallelization&lt;br /&gt;
&lt;br /&gt;
= Jupyter notebook: &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; =&lt;br /&gt;
&lt;br /&gt;
For those that heavily use jupyter notebooks and nothing else (which this author personally doesn’t encourage), the package &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; nicely works inside jupyter notebooks (but so does many of the other packages above like &amp;lt;code&amp;gt;joblib&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;, etc.). One nice this about this package is the verbosity, etc. works intellegently in the notebook. This package also works with &amp;lt;code&amp;gt;numba.jit&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;cython&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Before opening your &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; you’ll want to make sure the extension is enabled - which can be down from the command line with the following command or inside the &amp;lt;code&amp;gt;jupyter notebook&amp;lt;/code&amp;gt; extensions tab.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;ipcluster nbextension enable&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
And then you’ll need to go to the “IPython Clusters” tab in &amp;lt;code&amp;gt;jupyter noteook&amp;lt;/code&amp;gt; and start some parallel clusters.&lt;br /&gt;
&lt;br /&gt;
== &amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; parallelization ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import ipyparallel as ipp&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
For direct execution we define &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; and provide it with what it needs…&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;rc = ipp.Client()&lt;br /&gt;
dv = rc[:] # direct view&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
=== passing things to each cluster ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;ipyparallel&amp;lt;/code&amp;gt; provides ways for the user to pass only the necessary items to the the clusters (as to not overload the clusters with too many things).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol style=&amp;quot;list-style-type: decimal;&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;passing packages all together&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;with dv.sync_imports():&lt;br /&gt;
    from sklearn.linear_model import LinearRegression&lt;br /&gt;
    # getting dependencies on the clusters&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;passing variable over:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Below is just an example - we don’t need to pass &amp;lt;code&amp;gt;my_k&amp;lt;/code&amp;gt; since &amp;lt;code&amp;gt;dv&amp;lt;/code&amp;gt; will directly get it when we wnat to use it.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;dv[&amp;quot;my_k&amp;quot;] = my_k&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;p&amp;gt;we can also use the &amp;lt;code&amp;gt;%px&amp;lt;/code&amp;gt; magick to run lines of code on all servers. Given I think &amp;lt;code&amp;gt;numpy&amp;lt;/code&amp;gt; is cooler as &amp;lt;code&amp;gt;np&amp;lt;/code&amp;gt; I actually have to do it this way, not the &amp;lt;code&amp;gt;dv.sync_imports&amp;lt;/code&amp;gt; way.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%px import numpy as np&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== actual functionality ===&lt;br /&gt;
&lt;br /&gt;
The actual functionality is very similar to &amp;lt;code&amp;gt;multiprocess&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;multiprocessing&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;maps&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;apply&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== synchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_sync(my_simulation, my_k)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;201 ms ± 34.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
==== asynchronous ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;%%timeit&lt;br /&gt;
results = dv.map_async(my_simulation, my_k).get() # not clear why I needed to use .get() to get something returnable for async&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;209 ms ± 28.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
= additional topics and resources =&lt;br /&gt;
&lt;br /&gt;
# [https://ray.io/ &amp;lt;code&amp;gt;Ray&amp;lt;/code&amp;gt; python package] allows paralleilization across multiple computers&lt;br /&gt;
# [https://people.duke.edu/~ccc14/sta-663-2018/notebooks/S14A_Parallel_Programming_Introduction.html Duke’s statistical computing course] provides a set of examples for multiple python parallelization packages |&lt;/div&gt;</summary>
		<author><name>Ecucuzze</name></author>
	</entry>
</feed>