我们可以并行执行此任务吗? [英] Can we parallelize this task?

查看:94
本文介绍了我们可以并行执行此任务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个C字符串(以NULL字符常量终止的字符数组),我们必须找到字符串的长度.您能否提出一些方法来并行处理N个执行线程.我在划分子问题时遇到问题,因为访问不存在的数组位置会导致分割错误.

Given a C string (array of characters terminating with a NULL character constant), we have to find the length of the string. Could you please suggest some ways to parallelize this for N number of threads of execution. I am having problem dividing into sub-problems as accessing a location of the array which is not present will give segmentation fault.

编辑:我并不担心并行执行此任务是否会产生更大的开销.只想知道这是否可以完成(使用诸如openmp之类的方法)

EDIT: I am not concerned that doing this task in parallel may have much greater overhead or not. Just want to know if this can be done (using something like openmp etc.)

推荐答案

不行.因为每个步骤都需要知道先前的状态(我们在先前的char上遇到了null).您一次只能安全检查一个字符.

No it can't. Because each step requires the previous state to be known (did we encounter a null on the previous char). You can only safely check 1 character at a time.

想象一下,您正在翻过岩石,必须停在一个下面涂有白色油漆(无)的状态,否则您会死掉(又名seg断层等).

Imagine you are turning over rocks and you MUST stop at one with white paint underneath (null) or you will die (aka seg fault etc).

您不能让人们彼此领先",因为白色的油漆岩可能介于两者之间.

You can't have people "working ahead" of each other, as the white paint rock might be in between.

拥有多个人(线程/进程)只是将他们轮流转为下一轮摇滚.他们永远不会在同一时间翻过石头.

Having multiple people (threads/processes) would simply be them taking turns being the one turning over the next rock. They would never be turning over rocks at the same time as each other.

这篇关于我们可以并行执行此任务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆