In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use multi-core CPU to achieve parallel computing in Go language. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.
Before we begin, let's clarify two concepts. "multicore" refers to the effective use of the multicore of CPU to improve program execution efficiency. There is a difference between "parallel" and "concurrency", but they are actually two completely different concepts. "concurrency" is generally controlled by the CPU kernel through time slices or interrupts, and the right to use threads will be handed over when IO blocks or time slices run out, so as to process multiple tasks on one kernel. "parallelism" means that multiple processors or multi-core processors execute multiple tasks at the same time, and multiple tasks are scheduled at the same time, so one kernel cannot achieve parallelism, because only one task is scheduled at the same time.
Multi-process, multi-thread and cooperative program obviously belong to the category of "concurrency", which can realize the concurrent execution of the program. As to whether it supports "parallel" or not, it depends on whether the program running system is multi-core and whether the programming language can take advantage of the multi-core characteristics of CPU.
Let's take goroutine as an example to demonstrate how to effectively use "multicore" to achieve "parallel" execution of programs in the Go language. The specific implementation is to allocate the equivalent number of subroutines according to the number of CPU cores of the system, so that all cores are assigned to each kernel to execute in parallel. To view the number of system cores, take MacOS as an example, you can use the sysctl hw command to view the number of physical CPU cores and logical CPU cores respectively:
The number of physical CPU cores in my system is 4, and the number of logical CPU cores is 8. The so-called number of physical CPU cores refers to the number of CPU cores actually inserted into the physical slot, and the number of logical CPU cores refers to the number of CPU cores obtained by combining CPU multi-core and hyperthreading technology. The final core number is based on the number of logical CPU cores.
In addition, you can also get the number of CPU cores in the Go language by calling the runtime.NumCPU () method.
Next, we simulate a computing task that can be parallel: start multiple subprograms, and the number of subcoprograms is the same as the number of CPU cores, in order to make full use of multi-core parallel computing, and each subcoprogram is assigned to its part of the computing task. Finally, the results of different subprograms are accumulated again, so that the sum of all data can be obtained. Let's write the corresponding sample file parallel.go:
Package main
Import ("fmt", "runtime", "time")
Func sum (seq int, ch chan int) {defer close (ch) sum: = 0 for I: = 1; I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.