parallel computing hands-凯发k8网页登录
this video accompanies a hands-on workshop introducing you to parallel computing with matlab® and simulink®, so that you can solve computationally and data-intensive problems using multicore processors, gpus, and computer clusters. by working through common scenarios to parallelize matlab algorithms and run multiple simulink simulations in parallel, you will gain an understanding of parallel computing with matlab and simulink and learn about best practices.
along with the video, exercises and examples are provided to reinforce how to use parallel computing with matlab and simulink. workshop exercises and examples will vary in difficulty from simple parallel usage concepts to more advanced techniques.
highlights
• speeding up matlab applications with parallel computing
• running multiple simulink simulations in parallel
• gpu computing
• offloading computations and cluster computing
• working with large data sets
hi, everyone, and welcome to this workshop on parallel computing with matlab and simulink. parallel computing is an important topic because the problems that engineers and researchers face are growing larger and becoming more complex. in addition, as technology evolves, expectations have increased for faster and more efficient results.
parallel computing in matlab and simulink enables engineers, scientists, and researchers in any sector or industry to leverage the compute resources readily available to them without needing to be experts in parallel computing. here are some examples of real performance gains experienced by matlab customers who used matlab parallel computing tools to speed up their work. this workshop will guide you through the steps and tips to doing the same, and keep in mind that parallel competing hardware is becoming increasingly widespread and available.
multicore processors are the norm, and gpu devices capable of computation are becoming commonplace. in addition, access to cluster and cloud environments has been increasing, providing the ability to use computational resources beyond what is available on a typical workstation. whether you're planning to leverage multicore processors, compute clusters, or gpus, it is important to optimize your code so that you can receive even better performance improvements when you introduce parallel tools for additional computing power.
let's highlight some steps you can take to optimize your code. before modifying your code, you need to determine where to focus your efforts. perhaps the most critical tool to support this process is the profiler, which can help you find bottlenecks by telling you where your code is spending most of its execution time. improving those areas gives you the biggest performance boost for your efforts.
once you've located your areas of investment, you can use effective programming techniques like preallocation and vectorization to accelerate the execution of your matlab code. the matlab code analyzer can help advise you with this in addition to bringing issues and errors in your code to your attention. finally, you may also obtain speed ups by replacing parts of your matlab code with an automatically generated matlab executable, known as a mex function. you can do this using a separate product called matlab coder.
it's important to note that even without parallel computing toolbox, matlab provides implicit multicore support with its built in multi threading. a growing number of core matlab functions take advantage of underlying multi-threaded library support and other toolboxes leverage those benefits when they use core matlab functions. however, not every matlab function is able to be multi-threaded and any acceleration is limited to your local workstation. therefore, parallel computing tools enable you to receive benefits beyond these limitations.
put another way, parallel computing toolbox box enables direct control of your parallel resources. for example, parallel constructs like parfor let control which portions of your workflow are distributed to multiple cores. later on, we'll talk about how you can extend that level of control to resources on compute clusters by using matlab parallel server.
the following video clip will clearly demonstrate performance improvements obtained using parallel computing in matlab, specifically with parfor. we have three different scenarios where we run the same parameter sweep code in three different computing environments, a single desktop workstation, a cluster of 200 cores, and a cluster of 1,000 cores. as you saw for this problem, using 1000 cores provided a very significant speedup.
that being said, we should mention that throwing more cores at a problem doesn't always give you proportionally faster results. as a general rule of thumb, if your model or application is computationally intensive and you have a large number of independent iterations to complete, you can likely make efficient use of a large number of cores to speed up your overall execution time. having discussed the motivation for and usefulness of parallel computing, let's talk about how to utilize it in matlab.
we'll start by talking about using multiple cores on a desktop computer, where will also learn some parallel computing fundamentals like the concept of a worker and how a parfor like the concept of a worker, and how a parfor loop works. after that, we'll talk about using gpus followed by scaling up to a cluster or cloud environment. we'll then give some tips on using parallel computing with big data.
mathworks offers two parallel computing tools. we alluded several times to parallel computing toolbox, which we will cover now. later, we'll talk about matlab parallel server. those of you licensed to use parallel computing toolbox will install it alongside matlab. we'll use the term matlab client to refer to the machine with a tool box installed.
the toolbox will allow you to be more productive with your multicore processors by utilizing matlab computational engines called workers. these workers are controlled by your matlab session and allow you to use the full potential of your hardware to speed up your workflow. you can use workers interactively or send work for them to run in the background. workers form the basis for cpu-based parallel workflows.
when you have a collection of workers with interprocess communication, we call that a parallel pool. you can initialize and manage a parallel pool programmatically using matlab code, or interactively from this icon in the matlab desktop environment. parallel computing toolbox handles the work involved in dividing up tasks and computations and assigning them to workers in the parallel pool, thereby enabling your resources to perform parallel computing.
the behind the scenes work is all encapsulated in easy to use syntax. sometimes as simple as just changing one word, and you never have to leave the familiarity of the matlab desktop environment. in general, you should not run more matlab workers than the number of physical cores available to your machine, otherwise you are likely to have resource contention.
now that we've covered the basics of enabling parallel computing through workers, let's talk about what we can do with them. some parallel constructs in matlab are easier to get started with but offer less control. others require more knowledge of parallel computing but offer more granular control. we'll start with the easiest to use and work our way down. a large number of matlab toolboxes have automatic parallel support built into them.
if you find that a function has parallel support and it's contained in your bottleneck, you can accelerate your code with very little effort. here are examples of functions across different applications with automatic parallel support. the link at the bottom will give you the full list of what toolboxes and functions have automatic parallel support. similarly, a number of parallel enabled block sets in toolboxes for simulink can help you speed up your workflows with very little effort.
for example, simulink design optimization has one of the best integrations with parallel computing toolbox. you just enable a single checkbox, use parallel pool during optimization, and it will immediately speed up workflows like sensitivity analysis, response optimization, and parameter estimation. once again, you can use the link at the bottom to see the full list of automatic parallel support.
let's move on to the next level. if your bottleneck does not involve a function with automatic parallel support, there are plenty of parallel constructs available in matlab that give you more control over what and how something is paralyzed. the parallel computing team at mathworks is actively adding more constructs and improving existing ones. as we mentioned earlier, depending on the problem, parallel computing does not always give you proportional improvements, however, there are ideal problems for parallel computing where computationally intensive problems are just a matter of multiple tasks, iterations, or simulations that don't depend on each other to complete their calculations.
real world examples of such problems are monte carlo simulations, parameter sweeps, and design optimization, and the easiest way to address this challenge is to use parallel for loops. for example, let's say you want to run five iterations of your code. if you run it in a for loop, they run serially, one after the other. you wait for one to complete before moving to the next iteration. however, if they're all independent tasks with no dependencies or communication needed between individual iterations, you can distribute these tasks to separate workers and compute them in parallel at the same time.
this maximizes the utilization of the course on your machine and gets you the results sooner. parallel for loops are implemented using the parfor command. while requires parallel computing toolbox to be able to leverage workers for parallel processing, it will actually still run without it. that means you can share code that uses parfor with colleagues and collaborators who might not have access to parallel computing toolbox.
in that scenario, parfor will behave like a traditional for loop, albeit with a different order of iterations. in this example, we want to take a typical serial for loop and run it in parallel using our multicore processor. the iterations in this for loop are not dependent on one another and do not need to pass information between each other. all we have to do here is change the for loop to a parfor loop, and this will automatically run the iterations in parallel across multiple workers.
parfor will automatically distribute the tasks to the available workers and collect results upon completion. when changing for to parfor, you may need to make some adjustments to your code. the code analyzer will help to guide you through this process by informing you of what changes need to be made in order to run the parfor loop.
in this example, no warnings are displayed and no additional code changes are needed. in the second example with slightly different code, the code analyzer determines that there is an issue and brings it to our attention. the following illustration will give you more insight into what happens when parfor executes. in this example, matlab has access to three workers. they are assigned tasks to run, and once a worker has finished its current task, it can be assigned additional work.
finally, the results are collected and can be displayed in matlab. when matlab recognizes a name in a parfor loop as a variable, the variable is classified in one of several categories shown in the table on the right. two variable types that can have a significant impact on your runtime are slice variables and broadcast variable. a sliced variable is a variable whose value can be broken up into segments or slices, which are then operated on separately by different workers.
each iteration of the loop works on a different slice of the array. using slice variables can reduce the amount of required communication between a client and the workers. a broadcast variable is any variable other than the loop variable or a slice variable that does not change inside the loop. at the start of a parfor loop, the values of any broadcast variables are sent to all workers. that means that large broadcast variables can cost significant overhead in having to transfer them between the client and workers.
therefore, optimize parfor loops by trying to use a more slice variables and keep small any necessary broadcast variables to reduce parallel overhead. another common parallel construct is parfeval, short for parallel f eval. this parallel construct is similar to parfor loops because it utilizes parallel workers to run multiple tasks in parallel.
the difference is it operates only on functions and it is asynchronous or non-blocking with respect to matlab. unlike parfor loops, parfeval allows you to continue executing commands in matlab while the parallel work completes in the background. parfeval creates a queue of tasks that each execute a function on a parallel worker.
the queue is such that the next item in the queue is always executed on the next available worker in the pool, thereby preserving order of execution. after tasks are queued up for execution, you are free to use matlab on other tasks without having to wait on the queued tasks. when they are done, you can retrieve results of the computation using fetchnext.
you can also add or delete tasks from the queue. parfeval also distributes work to the parallel workers in a different manner than parfor. as you saw, instead of transferring groups of tasks to the parallel workers, parfeval transfers one task at a time. if your tasks or iterations have significantly different run times, parfeval will help avoid idle workers that can be caused by grouping tasks.
that being said, parfor will still most likely be your go to solution, but keep parfeval in mind if need to preserve the order of execution, if you need a parallel queue, or if you would like to keep using matlab while it performs computations in the background. the data queue allows you to pass data from the parallel workers back to the matlab client. one useful application of this is being able to view the progress of your parallel computation.
to get started, we construct the data cube and create a weight bar, which we will use to view our progress. we then specify what action will take place every time a worker triggers the data queue. in this case, we want to run a function and update weight bar, which will update the weight bar. we use the after each construct to trigger the end update weight bar function after each iteration in the parfor as indicated by the send construct.
you can also use after each with parfeval. as the parfor runs, the workers notify the client after their calculation is finished. this triggers the end update weight bar function, which updates the indicated progress of the parallel workers.
the key components of this workflow are data q. after each, send in the function you provide to run after each iteration. while you can technically use parfor within simulink, parfor was designed primarily for matlab and is not recommended to be used with simulink. instead, use parsim to run multiple simulations in parallel.
parsim distributes multiple simulations to multicore cpus to speed up overall simulation time. it automates the creation of parallel pools identifies file dependencies, and manages build artifacts. it also works in conjunction with a new simulation input object, which helps you setup all your simulation inputs in a convenient way, including variables, block parameters, and simulation configurations.
at this point, we've talked about automatic parallel support and common programming constructs. parallel computing toolbox also offers advanced parallel constructs for the most control of your resources, such as configuring parallel workers to communicate with one another and past data, splitting large matrices across the memory of multiple machines, and working with large repositories of data. since these topics may not have broad appeal, if you'd like to learn more, you can check the resources at the end of this presentation.
product documentation and technical support can also help you with your questions. parallel computing toolbox enables you to use nvidia gpus to accelerate ai, deep learning, and other computationally intensive analytics without having to be a [? qda ?] programmer. matlab has hundreds of functions with support for using nvidia gpus use and you'll be able to access multiple gpus on a desktop or cluster, generate [? qda ?] code and more.
to use a gpu on your workstation, you simply need matlab and parallel computing toolbox. you also need to make sure you have a supported nvidia gpu device with a recent graphics driver. it's best practice to ensure you are the latest driver for your device. gpus have hundreds-- sometimes thousands-- of cores with a very focused instruction set.
in matlab desktop or a single worker is all that is needed to take advantage of an entire gpu. in deep learning toolbox, functions like train network can use a gpu if you set a flag and have a suitable gpu. in addition, hundreds of functions in matlab and other toolboxes are overloaded to use a gpu if you supply a gpu array argument, which we will cover shortly.
the link at the bottom takes you to the documentation for running matlab functions on a gpu, where you can also find a list of gpu supported functions. not all problems are suited for the gpu. ideal problems for gpu computing are massively parallel and/or computationally intensive. massively parallel means that the computations can be broken down into hundreds or thousands of independent units of work.
you will see the best performance when all of the cores are kept busy, exploiting the inherent parallel nature of the gpu. computationally intensive means that the time spent on computation significantly exceeds the amount of time spent on transferring data to and from gpu memory. gpus have a high speed memory bus for data transfer within the gpu. however, the gpu has to use the much slower pci express bus to communicate with the cpu.
this means that your overall computational speedup will be reduced by the amount of time it takes to transfer data between devices, as required for your algorithm matlab developers have written [? qda ?] versions of key matlab and toolbox functions, which are presented as overloaded functions. the gpu version of a function will run when the input is in gpu memory. in the example to the right, we initially create a matrix on the cpu.
using gpu array, we send a copy of that matrix to the gpu. we then execute an fft function on that matrix, and note that even though there is no explicit instruction to use a gpu, matlab will see that the matrix resides on the gpu and we use the gpu instead of the cpu to perform the computation. this means that you can use your gpu for faster computation, while still using these same underlying code. after the computation is complete, you can gather the results and view them in matlab as normal. you can further accelerate your code using advanced gpu [? qda ?] and mex programming.
you can learn more by consulting product documentation and technical support. now that we have built up a fundamental understanding of parallel computing in matlab, let's discuss how we can start migrating our workflow to a cluster or cloud for even more computational power. after all, the problems or challenges you work on might need additional computational resources or memory beyond what is available on a single multicore desktop machine.
matlab parallel server enables you to scale up your desktop workflow to access the additional computational power and memory of multiple computers in a cluster, whether on premise in your organization, or on the cloud. while setting up the infrastructure might require support from your it staff, you can send jobs to a cluster without ever leaving the matlab desktop environment. and in the same spirit of other parallel computing tools in matlab, the code you developed on your desktop machine can be run on the cluster without having to recode your underlying algorithms. you can use a cluster for additional computing power or simply to free up your desktop computer for other work, which we'll soon discuss with a batch workflow.
to scale to a cluster, you'll need matlab and parallel computing toolbox box on your matlab client workstation, along with the licenses for any other tool box required by your code. on the cluster side, you only need matlab parallel server. instead of checking out toolbox licenses, each matlab parallel server worker dynamically licenses toolboxes and block sets to match the licenses from the submitting client.
you can select where to run your code by defining it programmatically in your code or through the matlab ui using cluster profiles. by default, you will have the local profile, which will run workers on your matlab client. you can create or import other profiles, which will point you to workers on remote hardware.
you can have multiple profiles for access to different cluster environments, and you could submit jobs to different profiles from the same matlab session. you can even have an interactive parallel pool on a cluster, which is useful for debugging and prototyping. note that your matlab client can only interact with one parallel pool at a time.
parallel computing in matlab supports cross platform submissions, which means that the operating system on which your matlab client runs can be different from the cluster operating system. since matlab syntax is the same on all platforms, there is no need to rewrite your algorithms. of course, you'll need to ensure that your code does not use hardcoded operating system specific file references parallel computing toolbox includes features like additional paths and attached files that help to resolve potential issues of sharing code and data with workers on a cluster.
depending on your system and network configuration, you can use workers on the cluster interactively with par pool, which is useful for prototyping and debugging. for long running jobs, you will want to transition to batch workflows, which we previously mentioned in passing. you can send your code using the batch command to run on remote hardware where matlab parallel server has been installed and configured.
using batch off loads of work from your computer so that your machine is no longer tied to the computation. that means you can do something else, put your machine to sleep, or even turn it off. by default, the command will request a single worker for serial computation, but you can include the pool argument to request multiple workers for parallel computation.
you can submit multiple batch jobs and the scheduler on the cluster side will schedule work as resources become available. you can check the state and progress of your job with these states in diary commands, which can use programmatically through the matlab command window and interactively through the job monitor. after the job is finished, you can retrieve the results in matlab or view artifacts generated from the job on the file system of the cluster.
you can also leverage batch processing with simulink. batchsim works similarly to the batch command by offloading your simulations to matlab parallel server on remote hardware, freeing up your desktop resources. note that if you're already using parsim in your code, you can change parsim to batchsim, specify the pool arguments, and run the simulations in batch. after completing, you have retrieved simulation results at your convenience for further processing on your desktop.
with a more complete understanding of parallel tools in matlab, we can see how matlab provides a single, high performance environment for working with big data on your desktop or on a cluster. there are matlab capabilities customized for both beginners and power users of big data applications. you'll be able to use constructs like data stores and tall arrays to access data that does not fit in memory.
you'll be able to use constructs like data stores and tall arrays to access data that does not fit in memory, use data from hadoop distributed file system, or hdfs, access cloud based storage, and create repositories of large amounts of images, spreadsheets, and custom files. while you'll have to learn about a few more functions, they all use the same intuitive matlab syntax with which you're already familiar. you can prototype algorithms quickly using small data sets and then scale up using these same code to big data sets stored in and process on large clusters.
tall arrays provide a way to visualize, parse, and analyze data, even if comprised of millions or billions of rows too big to fit in your machine's random access memory, or ram. they are backed by a data store, which is a repository of a large amount of files that are located on a machine, the cloud, or the cluster. many operations and functions are overloaded to work with tall arrays, using these same syntax as you would use with normal in memory matlab arrays. tall arrays wrap around one of these data stores and treat the entire set of data as one continuous table or array.
the underlying data store enables calculations to work through the array one piece at a time. all this is done behind the scenes, and to you as the user, you simply write what looks like normal matlab code. for example, if we build a tall array from multiple csv files containing tabular data, the resulting tall array is a tall table. even though this table doesn't fit in memory, we can use standard table functions like summary or dot references to access the columns and then use max, min, plus, minus, just as we would for a regular, in memory table.
since the pieces are processed independently, you can paralyze this using parallel computing toolbox and process several pieces at a time. naturally, you can scale up tall arrays across multiple computers using matlab parallel server. distributed arrays are a parallel data type that uses the memory of multiple machines to store variables that are too large to store on a single machine.
using distributed arrays, you can distribute your matrices across multiple machines and go beyond the capabilities of a single computer. you can prototype distributed array workflows on the desktop using parallel computing toolbox, and then scale up to a cluster, which uses matlab parallel server. in the same spirit of trying to make things easy and simple, a large number of standard matlab functions work with distributed arrays using the same syntax as normal arrays, again, as overloaded functions.
that means you can program and distribute an array algorithm in the same manner as an in-memory algorithm, and matlab will run the right version of the code based on the input data type. this enables you to take advantage of distributed computing without needing to be an expert in message passing. in this example, we develop and prototype an algorithm using distributed arrays on a local machine with a small data sample.
once we are confident that our algorithm works, we need only to change our cluster profile to run the same algorithm on the entire data set and scaled up on the cluster. using a data store, we can access multiple files, which each contain a portion of a matrix which we will use in our calculation. we use distributed with a data store to allow the data to be spread across the pool of workers in a way that allows the matrix to be processed as a single entity.
we can test locally with a small matrix stored in this single file or small set of files and then easily scale up by changing the profile to access a cluster and use a data store that accesses the entire data set, which will comprise a much larger matrix. note that outside of the circled code, the rest of the code is exactly the same. as we wrap up, we'll also leave you with some resources for additional information. hopefully, you've been able to see repeatedly the theme of matlab making it easy for you to use powerful computational resources and techniques so you can focus on your algorithms and research.
you don't need to be a parallel programming expert to get started, and you can always dig deeper into more advanced techniques if you want to get even more performance out of your resources. those resources include the hardware already available to your machine, as well as additional computational power from gpus or a cluster of machines. and whether you're developing serial or parallel algorithms, you can develop and prototype them locally using the familiar matlab syntax, then scale up to clusters or to the cloud without having to rewrite your underlying code.
here are some resources regarding the topics mentioned in this presentation. please feel free to reach out to us for any questions in regards to these topics and more. technical support or your account manager we'll also be glad to help answer any questions you may have.
related products
learn more
featured product
parallel computing toolbox
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 mathworks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- (español)
- (english)
- (english)
欧洲
- (english)
- (english)
- (deutsch)
- (español)
- (english)
- (français)
- (english)
- (italiano)
- (english)
- (english)
- (english)
- (deutsch)
- (english)
- (english)
- switzerland
- (english)
亚太
- (english)
- (english)
- (english)
- 中国
- (日本語)
- (한국어)