Code

2011年3月5日星期六

Coroutine


以前也听说过coroutine这个词,当时以为只是helper function的另外一种说法,故没有去研究个所以然。后来做unity3D project的时候有一次看到这个词,在文档里记录的例子是一个等待的程序,如下:

yield WaitForSeconds(5.0);


这个看起来不就是一个简单的wait吗,用一个loop一个timer就可以实现的一个东西而已。我当时这么认为。unity的doc也不是很详细,所以我也就没再去看。可是后来所UDK的时候遇到了一个叫latent function的东西,类似multi-threading吧,可以独立运行,然后在一段时间后返回。latent function也可以实现像sleep这样的功能。这时候我就想难道这个跟coroutine有什么关系,难道这是一个所以游戏引擎编程共有的功能。


UDK的文档里这样描述latent function:



While an actor is executing a latent function, that actor's state execution doesn't continue until the latent function completes. However, other actors, or the VM, may call functions within the actor. The net result is that all UnrealScript functions can be called at any time, even while latent functions are pending.


In traditional programming terms, UnrealScript acts as if each actor in a level has its own "thread" of execution. Internally, Unreal does not use Windows threads, because that would be very inefficient (Windows 95 and Windows NT do not handle thousands of simultaneous threads efficiently). Instead, UnrealScript simulates threads. 


这样看来我可以把coroutine或者latent function理解成一种模拟更高效的线程。在lua的文档里对coroutine解释的很详尽。coroutine的一个关键词是yield,yield相当于一般程序里面的return。区别就在于return是将原来方程的stack context都清除了,下次call这个方程的时候,一个新的stack layer又会建立。而yield把执行权交给calling function同时也save自己的context,下次再被call的时候可以接着之前的context执行。

这样可以模拟多线程。试想有好几个子方程被一个母方程控制在一个loop里面,然后子方程又在自己的loop里面完成一些任务,每loop一次call一次yield。母方程里的loop按顺序resume子方程,这样母方程每loop一次,子方程也一次loop 一次。

用coroutine比用thread的好处是coroutine不需要管理多线程会遇到的一些问题,比如说锁。但坏处我觉得是不能做太复杂的东西,因为这样会消耗大量的stack space,而且管理起来会很复杂。其实一些小的任务用coroutine就绰绰有余了,比如说walk()。

UDK里的latent function应该是coroutine的一个wrapper。它的功能比unity里的coroutine(貌似只能wait)多了两个,MoveTo和FinishAnim, 在使用这些function的时候我完全可以把它当作一个thread,但它是怎么实现的却是不得而知的。



2011年2月26日星期六

Just a note on CUDA AI setup

1. need to disable TDR: can just diable inside parallel nsight
2. need to disable WPFHDRAcceleration http://msdn.microsoft.com/en-us/library/aa970912.aspx

2011年2月24日星期四

找工作的一些想法

我现在觉得以CAP来划分工资的政策很Cheapo。

一个公司既然已经决定要一个人了,就是对他能力的肯定,对他今后对公司的贡献有信心。如果这个人的能力都to the standard了,还给他较低的pay的话,那就是不公平。感觉好像就是公司把“可以少付你工资”也当作一个条件,之所以招你是因为有这个advantage,这就是cheapo。而且如果招的人能够给公司带来同样的利益,为什么要给他比较低的工资,那样省钱为什么呢?

我觉得CAP只应该在筛选应聘者的时候使用,因为那只是一个general的指标。就像银行,太多人去应聘,看CAP就成了一个Reasonable而且fast的方法。如果招的人不多,招到的人连一个first class都没有,还拿CAP来划工资,就有点怎么说呢。。被鄙视的感觉。
大公司不能做的这么Cheapo,不然真的是没有什么前途了,因为很多人才都不会被重视。

我倒不是觉得钱多钱少,只是觉得自己值多少就是多少,不想被under-valuated.