What is Parallel Programming? Show In very simple terms, it is the use of multiple resources, in this case, processors, to solve a problem. This type of programming takes a problem, breaks it down into a series of smaller steps, delivers instructions, and processors execute the solutions at the same time. It is also a form of programming that offers the same results as concurrent programming but in less time and with more efficiency Advantages There are two major advantages to using this programming over concurrent programming. One is that all processes are sped up when using parallel structures, increasing both the efficiency and resources used in order to achieve quick results. Another benefit is that parallel computing is more cost efficient than concurrent programming simply because it takes less time to get the same results. Where is it Used? This type of programming can be used for everything from science and engineering to retail and research. It’s most common use from a societal perspective is in web search engines, applications, and multimedia technologies. Nearly every field in America uses this programming in some aspect, whether for research and development or for selling their wares on the web, making it an important part of computer science. Parallel computing is the future of programming and is already paving the way to solving problems that concurrent programming consistently runs into. https://www.bestcomputersciencedegrees.com/faq/what-is-parallel-programming Kita sering dengar Big Data Processing, Artificial Intelligence, Machine Learning, Data Science, dan hungry applications, semua itu memerlukan Teknik pemrograman parallel. Saat ini, pendaftar baru 7 mhs, silahkan bagi yang berminat bergabung. Kita akan praktek dengan Cluster System, Multicore Computers (64 cores) dan Nividia GPU tipe GEForce GTX 2080 Ti dengan 2944 CUDA cores. Detail CantumanAdvanced Search TextParallel Programming: Teknik dan Aplikasi Menggunakan Jaringan Workstation & Komputer ParalelBuku ini dibuat untuk mahasiswa ilmu komputer atau profesional di bidangnya. Pembahasan dalam buku ini mencakup teknik pemrograman paralel dengan cara penyampaian yang praktis, yang memampukan mahasiswa untuk membuat dan mengevaluasi pemrograman paralel mereka. Ketersediaan
Informasi Detil
Versi lain/terkaitTidak tersedia versi lain Last evening at Vancouver’s PHP meetup group I did a presentation on the topic of parallel processing with PHP. I received some good feedback on the talk so I’d like to share it with those of you who couldn’t attend the meetup. First of all quite a few people asked me what tools did Saem and I use to make up those cool slides. We used the open sourced reveal.js to make HTML5 based slides, and hosted the slides by node.js, and if you want to control the slides remotely with your smartphone, then socket.io is the node.js plugin you will need. So, what is ‘Parallel Processing’? In the context of computer science, parallel processing means “The simultaneous use of more than one CPU or processor core to execute a program or multiple computational threads” — Wikipedia. There are plenty of programming languages such as C, Java and Erlang that support multi-threading natively. Unfortunately, PHP is not one of them. Before we go ahead and start blaming PHP’s lack of multi-threading support, let’s not forget that PHP is a high level scripting language and the original purpose of it was to serve stateless HTTP requests, it’s supposed to be one and done, and it runs under Apache environment which is already multi-threaded. However, as the language became more and more popular and capable, developers started using PHP to build complex business logic other than just render HTML pages. It’s a natural evolvement so there is nothing wrong with that, but more and more advanced capabilities were demanded from PHP, and one of them is parallel processing. One use case at HootSuite is sending 10s of 1000s of scheduled social network messages at a specific time interval, for example at 8:00 AM. Loading all the messages scheduled at 8:00 AM and sending them out one by one sequentially is going to take hours, which is definitely not going to cut it for our users who expect delay of no more than a minute. So some form of parallel processing is needed so we can send them all out simultaneously. Doing this in PHP pretty much comes down to
So let’s take a look at each method, its options and their pros and cons. Multi-ThreadAs I mentioned early on PHP does not support multithread natively. But there is one PHP extension (experimental) called pthreads that allows you to do just that. I didn’t spend much time playing with pthreads because personally I think doing multithreading in PHP is just wrong. You can write beautiful thread safe code, but the moment you start using other PHP extensions, you have to think about if they are thread safe, and unfortunately a lot of those popular ones aren’t! (Update: Joe Watkins, the author of pthreads pointed out that pthreads is safe to use and my assumption is wrong. As I mentioned I didn’t play around with pthreads much and will likely stay away from multithreading in PHP, but if you are interested in trying it out, read Joe’s pthreads description here) Multi-ProcessWhile multithreading is out of the question, you can achieve parallel processing with running multiple PHP processes. I’ll show you three ways to do that:
Fork PHP comes with a few process control functions that allow you to fork child processes, which uses the underlying `fork()` system call. when `pcntl_fork` is called, a child process is initiated, which is a copy of parent process. What that means is child process can inherit parent variables and resources such as a MySQL connection or a file handle. You can fork multiple child processes and let them handle tasks in parallel. However this module is not available in Apache’s mod_php mode, so you can only do it in CLI or FastCGI mode. Besides, having children process sharing resources with parent is dangerous, for example a child can close a MySQL connection without parent knowing. Here is a simple code example that illustrate process forking Execute External Command Another way to create multiple processes is through `exec`, `exec_shell` or `passthru`, which run an external command, such as a PHP command, and create completely new and separate processes. The major difference among these three functions are,
One thing to be aware of is, executing external command is not a cheap operation, especially when you need to execute many in a loop. When you have hundreds of processes running on a server, the system will be super busy dealing with context switches, thus degrading overall performance. Piping So far we’ve looked at forking and executing external command to create processes. If you haven’t noticed yet, one common problem for both is that processes can’t communicate with each other. If you want to do some coordination between processes while they are running, then you are out of luck. So next I’ll show you one more lesser known way to deal with multiple process, which is called piping. It allows two processes sending instructions to each other through STDIN and STDOUT, much the same as linux pipe (|) command. The key PHP function is `proc_open`, which creates a new process and returns the process handle. It takes a bit of effort to make it work, but could be very powerful. Here are some code example to illustrate how piping works. Basically client sends ‘Hello’ to Worker, then worker takes ‘Hello’, appends ‘World’, and send the whole string back to client.
Distributed Parallel ProcessingSo far we have looked at theads, forking child process, creating new process, even letting processes communicate with each other. However these methods only apply when you are just dealing with one single server. You can only scale so far by adding CPUs and cores within a server, at some point you’ll need to go beyond one server and that, you will need distributed parallel processing. In a distributed world, some degree of network level programming is inevitable. Fortunately there are quite a few open sourced libraries and frameworks available so we don’t have to write socket level programs in PHP. In this blog I’ll show you how Gearman and ZeroMQ fit into the picture. Socket Programming But before I do that, why don’t we start with some fun stuff first! Let’s write some socket program that enables two processes communicating with each other over TCP! Yes, PHP does provide a few functions that allow you to do socket programming. Again, we have a client script that sends ‘Hello’, and worker script that takes ‘Hello’, appends ‘World’, and sends ‘Hello World’ back to the client.
Socket programming isn’t that hard right? Well, don’t be fooled by the simplicity of the code above because they are no where close to production ready! Think about what will happen when worker is not up? What will happen when the worker quits in the middle of execution? What will happen when both client and worker are normal but there is a network partition? There will be a gazillion of situations like this you’ll have to handle when writing socket code, and this is exactly why you should really appreciate open sources tools such as Gearman and ZeroMQ. Gearman Gearman was developed and open sourced by Danga, who also created the most popular distributed caching solution Memcached. A few tings about Gearman before I show some code examples:
We have used Gearman for a few years at HootSuite and overall we were happy with it, although we are not particular in love with centralized broker system for distributed processing, and that’s why we started looking into ZeroMQ a while ago and initiated the transition from Gearman to ZeroMQ. Let’s have a look at a simple Gearman code example before we get to ZeroMQ. Please note that to run the following code you have to have Gearman deamon (GearmanD) installed and started on localhost:4731, and Gearman PHP extension installed. And the example is the same Hello World example as above.
ZeroMQ ZeroMQ is the new favorite technology at HootSuite, please read my previous blog post here to find out why! Here is a summary of ZeroMQ characteristics.
ZeroMQ is one of the fast growing technical skillsets according to LinkedIn, so if you don’t know what it is yet, definitely check it out. It’s not a silver bullet that magically solves everything, but it does make building distributed systems awfully simpler. HootSuite skipped WS/SOAP, Thrift or even REST, and use ZeroMQ directly to build our internal SOA. The followings are some simple code examples illustrating how to use ZeroMQ to build the Hello World client/worker program similar to the examples above. Again, make sure you have ZeroMQ C library and the PHP extension installed.
What I suggest is, run the client code first before you start the worker, see what happens. Then start the worker and see what happens. While the client is looping through 100 iterations, stop the worker in the middle for a few seconds, then start it again and see what happens, do this a few times. Think about what you have achieved by writing ~30 lines of code, without a single line for error handling, and also remember there isn’t any message broker such as RabbitMQ or Gearman in the middle. — — — — — — — — - Intrigued? Interested? Come build awesome products with other talented engineers at HootSuite. Apply here! |