- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 gcc 中使用 pthreads。简单的代码示例将线程数“N”作为用户提供的输入。它将一个长数组拆分为 N 个大小大致相等的子 block 。每个子 block 都由单独的线程写入。
此示例的虚拟处理实际上涉及为每个数组索引休眠一段固定的时间,然后将一个数字写入该数组位置。
代码如下:
/******************************************************************************
* FILE: threaded_subblocks_processing
* DESCRIPTION:
* We have a bunch of parallel processing to do and store the results in a
* large array. Let's try to use threads to speed it up.
******************************************************************************/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#define BIG_ARR_LEN 10000
typedef struct thread_data{
int start_idx;
int end_idx;
int id;
} thread_data_t;
int big_result_array[BIG_ARR_LEN] = {0};
void* process_sub_block(void *td)
{
struct thread_data *current_thread_data = (struct thread_data*)td;
printf("[%d] Hello World! It's me, thread #%d!\n", current_thread_data->id, current_thread_data->id);
printf("[%d] I'm supposed to work on indexes %d through %d.\n", current_thread_data->id,
current_thread_data->start_idx,
current_thread_data->end_idx-1);
for(int i=current_thread_data->start_idx; i<current_thread_data->end_idx; i++)
{
int retval = usleep(1000.0*1000.0*10.0/BIG_ARR_LEN);
if(retval)
{
printf("sleep failed");
}
big_result_array[i] = i;
}
printf("[%d] Thread #%d done, over and out!\n", current_thread_data->id, current_thread_data->id);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
if (argc!=2)
{
printf("usage: ./a.out number_of_threads\n");
return(1);
}
int NUM_THREADS = atoi(argv[1]);
if (NUM_THREADS<1)
{
printf("usage: ./a.out number_of_threads (where number_of_threads is at least 1)\n");
return(1);
}
pthread_t *threads = malloc(sizeof(pthread_t)*NUM_THREADS);
thread_data_t *thread_data_array = malloc(sizeof(thread_data_t)*NUM_THREADS);
int block_size = BIG_ARR_LEN/NUM_THREADS;
for(int i=0; i<NUM_THREADS-1; i++)
{
thread_data_array[i].start_idx = i*block_size;
thread_data_array[i].end_idx = (i+1)*block_size;
thread_data_array[i].id = i;
}
thread_data_array[NUM_THREADS-1].start_idx = (NUM_THREADS-1)*block_size;
thread_data_array[NUM_THREADS-1].end_idx = BIG_ARR_LEN;
thread_data_array[NUM_THREADS-1].id = NUM_THREADS;
int ret_code;
long t;
for(t=0;t<NUM_THREADS;t++){
printf("[main] Creating thread %ld\n", t);
ret_code = pthread_create(&threads[t], NULL, process_sub_block, (void *)&thread_data_array[t]);
if (ret_code){
printf("[main] ERROR; return code from pthread_create() is %d\n", ret_code);
exit(-1);
}
}
printf("[main] Joining threads to wait for them.\n");
void* status;
for(int i=0; i<NUM_THREADS; i++)
{
pthread_join(threads[i], &status);
}
pthread_exit(NULL);
}
然后我编译它
gcc -pthread threaded_subblock_processing.c
然后我像这样从命令行调用它:
$ time ./a.out 4
当我增加线程数时,我发现速度有所提高。使用 1 个线程,该过程仅需 10 秒多一点。这是有道理的,因为我为每个数组元素睡 1000 微秒,并且有 10,000 个数组元素。接下来,当我转到 2 个线程时,它会下降到 5 秒多一点,依此类推。
我不明白的是,即使我的线程数超过了我计算机上的内核数,我的速度仍然提高了!我有 4 个内核,所以我预计 >4 个线程不会加速。但是,令人惊讶的是,当我运行时
$ time ./a.out 100
我得到了 100 倍的加速,并且处理在大约 0.1 秒内完成!这怎么可能?
最佳答案
一些一般背景
很多事情都会减慢程序的进度,但一般来说,您可以将减慢点(也称为热点)分为两类:
CPU Bound:在这种情况下,处理器正在执行一些繁重的数字运算(如三角函数)。如果 CPU 的所有内核都在执行此类任务,则其他进程必须等待。
内存限制:在这种情况下,处理器正在等待从硬盘或 RAM 中检索信息。由于这些通常比处理器慢几个数量级,因此从 CPU 的角度来看,这需要永远。
但您也可以想象进程必须等待的其他情况,例如网络响应。
在许多这些受内存/网络限制的情况下,可以在内存向 CPU 爬行时将线程“暂停”并同时执行其他有用的工作。如果这一点做得好,那么多线程程序可以很好地胜过它的单线程程序。 Node.js 利用这种异步编程 技术来实现良好的性能。
您的问题
现在,回到您的问题:您有多个线程在运行,但它们既不执行 CPU 密集型工作也不执行内存密集型工作:没有太多时间可以占用。事实上, sleep 功能本质上是在告诉操作系统没有工作正在完成。在这种情况下,操作系统可以在您的线程休眠时在其他线程中工作。因此,表观性能自然会显着提高。
请注意,对于低延迟应用程序,例如 MPI,busy waiting 有时是 used 而不是 sleep 函数。在这种情况下,程序会进入一个紧密循环并反复检查条件。从外部看,效果看起来相似,但 sleep 不使用 CPU,而忙等待使用约 100% 的 CPU。
关于c - 多线程如何提供大于内核数量的加速因子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44958503/
我有这种格式的data.frame: 'data.frame': 244 obs. of 1 variable: $ names: Factor w/ 244 levels "ERA","BA
这就是问题: write a Java Program that accepts a String and an integer stretch factor P as parameters and
该示例显示了不同工厂的产量测量值,第一列表示工厂最后一列是生产量。 factory % mutate(factory=fct_lump(factory,2)) factory produc
我正在使用分类变量运行回归并遇到 this question .在这里,用户想要为每个虚拟对象添加一列。这让我很困惑,因为我虽然列有很长的数据,包括使用 as.factor() 存储的所有虚拟数据。相
假设在 R 中有一个 Data.Frame 对象,其中所有字符列都已转换为因子。然后我需要“修改”与数据帧中某一行相关联的值——但将其编码为一个因子。我首先需要提取一行,所以这就是我正在做的。这是一个
利用下面的可重现数据, dat head(dat) Bin Number 1 1 3 2 1 5 3 1 4 4 1 5 5 1
我有一组包含多个变量的数据。其中一个变量 - 阶乘包含组的名称 - A、B、C 等。其余变量是数字。 > data1 Group Value 1 A 23 2 A
我有一组编码为二项式的变量。 Pre VALUE_1 VALUE_2 VALUE_3 VALUE_4 VALUE_5 VALUE_6 VALUE_7 VALUE_8 1 1 0
我的问题与 this one 非常相似和 this other one ,但我的数据集有点不同,我似乎无法使这些解决方案起作用。如果我误解了什么并且这个问题是多余的,请原谅。 我有一个这样的数据集:
我一直在尝试生成一个带有离散 x 变量的堆积面积图(因为我想显示财政年度,即“2013/14”,而不是日历年)。但是,将 x 轴变量转换为一个因子会阻止在最终图表中呈现 geom。 有解决办法吗? l
只是一个简单的问题来确认我的想法, 使用负载因子 1.0 的哈希表的复杂性将是二次时间,用以下符号 O(n^2) 表示。 这是因为必须不断调整大小并一遍又一遍地插入。如果我错了,请纠正我。 谢谢 最佳
我正在尝试使用 kaggle 的一些数据集进行房价预测。 这是我的代码 library(ggplot2) dataset=read.csv('train(1).csv') dataset_test=r
我正在用 Angular 构建一个类似咆哮的 UI。我想将其公开为工厂(或服务),以使其在我的 Controller 中可用。调用 Growl.add 将导致 DOM 发生变化,所以看起来我应该有一个
我正在尝试将 pandas 数据框的一列转换为因数,因为我试图在 R 中调用的函数需要因数。 pandas2ri.activate() #second column of labels has
我正在尝试使用 plotly 绘制一个以字符串(组合数)作为 x 轴的条形图。 (“1”、“2”、“3”、“4 - 5”、“6 - 8”、“9 - 13”、“14 - 21”、“22 - 34”、“3
我有一个包含 NA 的数据集。 此外,它还有一些列需要factors()。 我正在使用 caret 包中的 rfe() 函数来选择变量。 似乎 rfe() 中的 functions= 参数使用 lmF
我有一个 .csv 文件,其中每个字段用于日期时间、日期和时间。 最初它们都是字符字段,我已经相应地转换了它们。 在我的代码结束时,如果我这样做: str(data) 我会得到 datetime: P
我有一个如下所示的数据集: data.flu data.flu chills runnyNose headache fever flu 1 1 0 M
我正在使用 QMainWindow 在 C++ 中手动布置 Qt 应用程序。我希望在屏幕底部有两个并排停靠的小部件,但我希望它们具有不成比例的宽度。目前,我只能让它们具有相同的宽度。有没有办法设置拉伸
我需要通过在两个主机(2 个 Java 进程)之间发送合成调用来计算 VOIP 质量。我应该找出 MOS、抖动和 R 因子(VOIP 质量指标)。根据目前的研究,我发现我应该在两台主机之间发送 RTP
我是一名优秀的程序员,十分优秀!