我想对我的内核代码(1维数据)应用reduce:
__local float sum = 0;
int i;
for(i = 0; i < length; i++)
sum += //some operation depending on i here;
Run Code Online (Sandbox Code Playgroud)
我想要有n个线程(n =长度),最后有1个线程来计算总和,而不是只有1个线程执行此操作.
在伪代码中,我希望能够写出这样的东西:
int i = get_global_id(0);
__local float sum = 0;
sum += //some operation depending on i here;
barrier(CLK_LOCAL_MEM_FENCE);
if(i == 0)
res = sum;
Run Code Online (Sandbox Code Playgroud)
有办法吗?
我总和有竞争条件.
parallel-processing multithreading reduction opencl race-condition
我有一个线程安全类容器:
public class Container {
private int x;
...
public synchronized int getX();
public synchronized void setX(int x);
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个容器列表:
List<Container> containers;
Run Code Online (Sandbox Code Playgroud)
我想迭代列表,在每次迭代时获取容器的锁,并且只在循环结束时释放所有锁.像这样的东西:
for(Container c : containers) {
c.lock();
//do stuff
}
for(Container c : containers)
c.unlock();
Run Code Online (Sandbox Code Playgroud)
我仍然希望其他线程能够继续使用未锁定容器的getX和setX方法,但出于各种原因,我不希望允许已经分析过的容器.
你知道那个java代码吗?
更好的想法也受到赞赏.
如果我只是做这样的事情:
synchronized(taskQueue) { //taskQueue is a BlockingQueue
taskQueue.drainTo(tasks); //tasks is a list
}
Run Code Online (Sandbox Code Playgroud)
我是否确信并发调用taskQueue.put()
不能taskQueue.take()
在同步块内执行?
换句话说,我是否使rainTo() 方法成为原子方法?
或者更一般地说,如何使线程安全操作的组合成为原子的?
例子:
if(taskQueue.size() == 1) {
/*Do a lot of things here, but I do not want other threads
to change the size of the queue here with take or put*/
}
//taskQueue.size() must still be equal to 1
Run Code Online (Sandbox Code Playgroud) 让我们采用我能想到的最简单的xquery函数:
declare function local:identityFunction($v as xs:integer)
{
return ($v)
};
Run Code Online (Sandbox Code Playgroud)
我在哪里申报?
我正在尝试使用exists-db和basex,但是如果我在查询处理器窗口中编写它,它们会给我一些错误(尽管正常的xqueries工作).
例如,basex抱怨以下消息:"Expecting expression".
这是xml代码:
<LinearLayout
android:id="@+id/mainLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="@color/black" >
<ScrollView
android:id="@+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/teal"
android:layout_weight=".50">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/addButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+"
android:gravity="center"
android:layout_weight=".20"
/>
<TextView
android:id="@+id/totalHoursLabel"
android:text="Ore Totali"
android:background="@color/gray"
android:textColor="@color/red"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".10"
/>
<TextView
android:id="@+id/totalHoursView"
android:text="00:00"
android:background="@color/white"
android:textColor="@color/black"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".20"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的问题是当我尝试将视图添加到ScrollView或他的内部LinearLayout时,我的程序崩溃了.
这是我的java代码:
super.onCreate(savedInstanceState);
LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout1);
TextView tv = new TextView(this);
tv.setText("testView");
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
ll.addView(tv);
setContentView(R.layout.activity_main);
Run Code Online (Sandbox Code Playgroud)
如果我创建一个新的android项目这个代码(只有一个relativeLayout和xml文件中的其他所有空)工作正常.
我认为findViewById方法或我的xml文件存在一些问题. …
据我了解(如果我错了,请纠正我),OpenGL api将程序员在源代码中编写的函数调用转换为图形卡的特定gpu驱动程序调用。然后,gpu驱动程序便能够通过某些硬件接口(例如PCIe,AGP或PCI)将指令和数据真正发送到图形卡。
我的问题是,因为基本上只有3种类型的物理连接(PCIe,AGP和PCI),openGL是否知道如何与不同的图形卡交互?
我想这不是那么简单,因为我总是读到不同的图形卡具有不同的驱动程序,所以驱动程序不仅是使用物理接口的一种方式,而且还具有使图形卡能够执行不同类型的驱动程序的目的。命令(特定于供应商)。
我只是不了解大局。
我希望能够写出这样的东西:
Fruit f1 = new Apple();
Fruit f2 = new Orange();
int res = f1.compareTo(f2);
Run Code Online (Sandbox Code Playgroud)
在fruit类中实现Comparable接口,如下所示:
public class Fruit<T> implements Comparable<? extends T> {
int compareTo(T other) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
似乎没有用.我猜在通配符中有关键字super的一些技巧...
我读到使用带有这样的超级通配符:
public class MyClass <T extends Comparable<? super T>> {
...
}
Run Code Online (Sandbox Code Playgroud)
代替:
public class MyClass <T extends Comparable<T>> {
...
}
Run Code Online (Sandbox Code Playgroud)
可以让这个班级"更通用",但我不明白为什么.
有人能提供一些具体的例子吗?
我有一个等待线程:
synchronized(sharedCounter) {
while(sharedCounter > 0) {
sharedCounter.wait(60000); //wait at most 1 minute
/*if it wakens from the end of the timeout, it should break the loop
or it could potentially restart the timeout*/
}
}
Run Code Online (Sandbox Code Playgroud)
一个可以通知的线程:
synchronized (sharedCounter) {
if(sharedCounter == 0)
sharedCounter.notify();
}
Run Code Online (Sandbox Code Playgroud)
如何区分通知和超时?
我可以这样做:
synchronized(sharedCounter) {
while(sharedCounter > 0) {
sharedCounter.wait(60000);
if(sharedCounter == -1) { //if it was a notify()
//I could save the fact that it was a notify() here
break;
}
//Otherwirse, assume it was …
Run Code Online (Sandbox Code Playgroud)