http://blog.csdn.net/duanbingnan/archive/2007/10/30/1856105.aspx
一、预处理的由来:
在C++的历史发展中,有很多的语言特征(特别是语言的晦涩之处)来自于C语言,预处理就是其中的一个。C++从C语言那里把C语言预处理器继承过来(C语言预处理器,被Bjarne博士简称为Cpp,不知道是不是C Program Preprocessor的简称)。
二、常见的预处理功能:
预处理器的主要作用就是把通过预处理的内建功能对一个资源进行等价替换,最常见的预处理有:文件包含,条件编译、布局控制和宏替换4种。
文件包含:#include 是一种最为常见的预处理,主要是做为文件的引用组合源程序正文。
条件编译:#if,#ifndef,#ifdef,#endif,#undef等也是比较常见的预处理,主要是进行编译时进行有选择的挑选,注释掉一些指定的代码,以达到版本控制、防止对文件重复包含的功能。
布局控制:#progma,这也是我们应用预处理的一个重要方面,主要功能是为编译程序提供非常规的控制流信息。
宏替换: #define,这是最常见的用法,它可以定义符号常量、函数功能、重新命名、字符串的拼接等各种功能。
三、预处理指令:
预处理指令的格式如下:
# directive tokens
#符号应该是这一行的第一个非空字符,一般我们把它放在起始位置。如果指令一行放不下,可以通过\进行控制,例如:
#define Error if(error) exit(1) 等价于
#define Error \
if(error) exit(1)
不过我们为了美化起见,一般都不怎么这么用,更常见的方式如下:
# ifdef __BORLANDC__
if_true<(is_convertible<Value,named_template_param_base>::value)>::
template then<make_named_arg, make_key_value>::type Make;
# else
enum { is_named = is_named_parameter<Value>::value };
typedef typename if_true<(is_named)>::template
then<make_named_arg, make_key_value>::type Make;
# endif
下面我们看一下常见的预处理指令:
#define 宏定义
#undef 未定义宏
#include 文本包含
#ifdef 如果宏被定义就进行编译
#ifndef 如果宏未被定义就进行编译
#endif 结束编译块的控制
#if 表达式非零就对代码进行编译
#else 作为其他预处理的剩余选项进行编译
#elif 这是一种#else和#if的组合选项
#line 改变当前的行数和文件名称
#error 输出一个错误信息
#pragma 为编译程序提供非常规的控制流信息
下面我们对这些预处理进行一一的说明,考虑到宏的重要性和繁琐性,我们把它放到最后讲。
四、文件包含指令:
这种预处理使用方式是最为常见的,平时我们编写程序都会用到,最常见的用法是:
#include <iostream> //标准库头文件
#include <iostream.h> //旧式的标准库头文件
#include "IO.h" //用户自定义的头文件
#include "../file.h" //UNIX下的父目录下的头文件
#include "/usr/local/file.h" //UNIX下的完整路径
#include "..\file.h" //Dos下的父目录下的头文件
#include "\usr\local\file.h" //Dos下的完整路径
这里面有2个地方要注意:
1、我们用<iostream>还是<iostream.h>?
我们主张使用<iostream>,而不是<iostream.h>,为什么呢?我想你可能还记得我曾经给出过几点理由,这里我大致的说一下:首先,.h格式的头文件早在98年9月份就被标准委员会抛弃了,我们应该紧跟标准,以适合时代的发展。其次,iostream.h只支持窄字符集,iostream则支持窄/宽字符集。
还有,标准对iostream作了很多的改动,接口和实现都有了变化。最后,iostream组件全部放入namespace std中,防止了名字污染。
2、<io.h>和"io.h"的区别?
其实他们唯一的区别就是搜索路径不同:
对于#include <io.h> ,编译器从标准库路径开始搜索
对于#include "io.h" ,编译器从用户的工作路径开始搜索
五、编译控制指令:
这些指令的主要目的是进行编译时进行有选择的挑选,注释掉一些指定的代码,以达到版本控制、防止对文件重复包含的功能。
使用格式,如下:
1、
#ifdef identifier
your code
#endif
如果identifier为一个定义了的符号,your code就会被编译,否则剔除
2、
#ifndef identifier
your code
#endif
如果identifier为一个未定义的符号,your code就会被编译,否则剔除
3、
#if expression
your code
#endif
如果expression非零,your code就会被编译,否则剔除
4、
#ifdef identifier
your code1
#else
your code2
#endif
如果identifier为一个定义了的符号,your code1就会被编译,否则yourcode2就会被编译
5、
#if expressin1
your code1
#elif expression2 //呵呵,elif
your code2
#else
your code3
#enif
如果epression1非零,就编译your code1,否则,如果expression2非零,就编译your code2,否则,就编译your code3
其他预编译指令
除了上面我们说的集中常用的编译指令,还有3种不太常见的编译指令:#line、#error、#pragma,我们接下来就简单的谈一下。
#line的语法如下:
#line number filename
例如:#line 30 a.h 其中,文件名a.h可以省略不写。
这条指令可以改变当前的行号和文件名,例如上面的这条预处理指令就可以改变当前的行号为30,文件名是a.h。初看起来似乎没有什么用,不过,他还是有点用的,那就是用在编译器的编写中,我们知道编译器对C++源码编译过程中会产生一些中间文件,通过这条指令,可以保证文件名是固定的,不会被这些中间文件代替,有利于进行分析。
#error语法如下:
#error info
例如:#ifndef UNIX
#error This software requires the UNIX OS.
#endif
这条指令主要是给出错误信息,上面的这个例子就是,如果没有在UNIX环境下,就会输出This software requires the UNIX OS.然后诱发编译器终止。所以总的来说,这条指令的目的就是在程序崩溃之前能够给出一定的信息。
#pragma是非统一的,他要依靠各个编译器生产者,例如,在SUN C++编译器中:
// 把name和val的起始地址调整为8个字节的倍数
#progma align 8 (name, val)
char name[9];
double val;
//在程序执行开始,调用函数MyFunction
#progma init (MyFunction)
预定义标识符
为了处理一些有用的信息,预处理定义了一些预处理标识符,虽然各种编译器的预处理标识符不尽相同,但是他们都会处理下面的4种:
__FILE__ 正在编译的文件的名字
__LINE__ 正在编译的文件的行号
__DATE__ 编译时刻的日期字符串,例如: "25 Dec 2000"
__TIME__ 编译时刻的时间字符串,例如: "12:30:55"
例如:cout<<"The file is :"<<__FILE__"<<"! The lines is:"<<__LINE__<<endl;
预处理何去何从
如何取代#include预处理指令,我们在这里就不再一一讨论了。
C++并没有为#include提供替代形式,但是namespace提供了一种作用域机制,它能以某种方式支持组合,利用它可以改善#include的行为方式,但是我们还是无法取代#include。
#progma应该算是一个可有可无的预处理指令,按照C++之父Bjarne的话说,就是:"#progma被过分的经常的用于将语言语义的变形隐藏到编译系统里,或者被用于提供带有特殊语义和笨拙语法的语言扩充。”
对于#ifdef,我们仍然束手无策,就算是我们利用if语句和常量表达式,仍然不足以替代她,因为一个if语句的正文必须在语法上正确,满足类检查,即使他处在一个绝不会被执行的分支里面。
本打算把FLUENT的后台执行功能调通以后,可以远程提交作业,并关闭本地机。搞了半天,发现,原来不能关,一关程序跟着死!
不过总算搞通了BATCH提交作业——可以预先设定计算参数,存在input文件里,而不用不停跟踪程序。
BATCH操作:
BATCH是FLUENT 提供的无图形界面启动功能,它将把FLUENT计算过程产生的文件存储在文件内,为不同线程通过远程登录用户监视计算过程。当前就用了很简单的功能:
;读入数据
rc temp.cas
rd temp.dat
;非定常问题求解
/solve/dual-time-iterate
10
500
;输出
/file/write-case-data fin.cas
exit
yes
一个网站上的例子
rc def.cas
rd def.dat
solve/set/time-step 0.01
solve/set/reporting-interval 20
solve/dual-time-iterate 10
it 100
wd def10.dat
solve/set/time-step 0.01
solve/set/reporting-interval 20
solve/dual-time-iterate 10
it 100
wd def20.dat
solve/set/time-step 0.01
solve/set/reporting-interval 20
solve/dual-time-iterate 10
it 100
wd def30.dat
solve/set/time-step 0.01
solve/set/reporting-interval 20
solve/dual-time-iterate 10
it 100
wd def40.dat
solve/set/time-step 0.01
solve/set/reporting-interval 20
solve/dual-time-iterate 10
it 100
wd def50.dat
solve/set/time-step 0.01
solve/set/reporting-interval 20
solve/dual-time-iterate 10
it 100
wd def60.dat
solve/set/time-step 0.01
solve/set/reporting-interval 20
solve/dual-time-iterate 10
it 100
wd def70.dat
solve/set/time-step 0.01
solve/set/reporting-interval 20
solve/dual-time-iterate 10
it 100
wd def80.dat
solve/set/time-step 0.01
solve/set/reporting-interval 20
solve/dual-time-iterate 10
it 100
wd def90.dat
solve/set/time-step 0.01
solve/set/reporting-interval 20
solve/dual-time-iterate 10
it 100
wd def100.dat
exit
From: http://lcni.uoregon.edu/~mark/Stat_mech/thermodynamic_entropy_and_information.html
The connection between thermodynamic entropy and information
The bottom line is that thermodynamic entropy is best understood not as a property or macroscopic state of matter (like mass, temperature, or pressure), but as a lack of knowledge of the detailed configuration of matter. In particular, thermodynamic entropy is a measure of our lack of information about the microstate of a closed system of matter near equilibrium. To make this concrete, I'll compare two similar simple systems, one of particles and one of bits. Although the concept of entropy in classical thermodynamics was elucidated long before information theory was developed, thermodynamic entropy can be viewed as a straight-forward application of information theory to a physical problem.
There are many other fine discussions of this topic, but few that strip it down to a simple example. A more in-depth, but more technical, discussion of the same topic is at Entropy in thermodynamics and information theory. But this discussion, and others have the same bottom line, with only a variation of language:
"....it should be remembered that Gibb's statistical mechanical entropy is only one application of information theory to physical systems, relevant when the particular 'message' not yet communicated is the underlying microstate of the physical system."
The 'message' in thermodynamics, the microstate of a physical system, will never be communicated as it is inaccessible to observation or transmission. A good diagram illustrating this idea of "physical information" is in M. P. Franks paper "Physical limits of Computing".
Consider a perfectly insulated 2-D box of simple particles. The
macrostate of an ideal gas can be specified by the total energy E, number of particles N and volume V. There are a large but finite number of possible microstates that are all consistent with this system's single, and unchanging, macro-state:
Ludwig Boltzmann's leap of imagination was that the number of possible microstates, Ω, was finite, and in some sense a particle's state is discrete. But it wasn't until quantum mechanics was developed that this was clarified and shown to be strictly true.
Henri Poincairé and others showed that such an ideal particle system would necessarily cycle through all possible microstates, and that each would be visited with equal probability. The same holds for all practical purposes in real physical systems; no state or group of states is favored.
|
Any one of these microstate is equally likely to be the actual microstate (near equilibrium) and we have no way of knowing which is the actual microstate. And we never will. This lack of information is not because we haven't examined the system closely; it reflects the inaccessibility of this information near equilibrium. But we
can count how many microstates are possible.
The thermodynamic entropy,
S, for this case is:
S/k = log(Ωp) Ωp = number of equally probable microstates, k = Boltzmann's constant
Boltzmann's form of this equation is S = k ln(Ωp), where Boltzmann's constant has SI units of JK-1. Because thermodynamic entropy is dependent on the energy and temperature of of the system, it was convenient to use this proportionality constant if these variables are measured or derived. An alternative, used here, is to normalize thermodynamic variables such that the proportionallity constant is defined as 1. From Entropy in thermodynamics and information theory:
"The presence of Boltzmann's constant k in the thermodynamic definitions is a historical accident, reflecting the conventional units of temperature. It is there to make sure that the statistical definition of thermodynamic entropy matches the classical entropy of Clausius, thermodynamically conjugate to temperature. For a simple compressible system that can only perform volume work, the first law of thermodynamics becomes
But one can equally well write this equation in terms of what physicists and chemists sometimes call the 'reduced' or dimensionless entropy, σ = S/k, so that
Just as S is conjugate to T, so σ is conjugate to kT (the energy that is characteristic of T on a molecular scale)."
Writing the equation in this way doesn't change thermodynamics, or its expression in information theoretic terms.
|
This statistical measure of thermodynamic entropy quantifies the uncertainty about which microstate is occupied. The higher the number of equally probable possibilities, the more uncertainty. Near equilibrium the system has a maximum entropy, because there are the most possible microstates near equilibrium. For example, there are very few possibilities for all the particles clumped in one corner of our insulated box but many possible ways they can be roughly evenly distributed across the box.
Compare this with a set of 2-D 4 x 4 arrays of bits (images in this case, each one a kind of message), each with the same macro-state specified by the number of bits (N = 16, represented by black or white squares). Note that the number of bits, N, is the same in each instance, although all combinations of black and white are in the set. If an acquaintance is to send you an image/message of this form (a 16-bit email, for example), and you have no prior information about which image/message is to be sent, then each of a
countable number (65,536) of images/messages is equally probable.
The information theory entropy (Shannon entropy), H, for this case is defined as:
H = log(Ωp) , Ωp = number of equally probable microstates
The entropy H quantifies the uncertainty about what message is to be received. The higher the number of equally probable possibilities, the more entropy. The image/message has a maximum of entropy before it is received. But after it is received and read, there is no longer any uncertainty; there is only one possible microstate, the image/message itself; Ωp = 1 and H = 0.
If a single one of these arrays is received as an image/message, the information, I, contained in the image/message is:
I = -log(1/Ωs) = log(Ωs) , Ωs = number of equally probable microstates consistent with the message macro-state
If the microstates are not equally probable, these formulas for S, H and I need to be modified. They become weighted sums over all possible states, where the probability of each state is the weighting factor. See Entropy in thermodynamics and information theory.
|
The probability of this particular image/message being sent is 1/Ω
s. The larger the number of possibilities, the more uncertainty is resolved, or entropy reduced, when the particular image/message is received. Information is a measure of how much an image/message (an observed microstate) tells us, by comparison with the number of other messages it could have been (those consistent with the image/message's macrostate).
| Here's the math. For this 16 bit message with 65,536 possibilities, a single message contains I = -log2(1/65,536) bits = -log2(2-16 ) bits = 16 bits. This is the amount the message was "surprising", or how much our uncertainty (entropy) was reduced -- it could have been a lot of things but it was this singular message. But this result -- 16 bits of information is contained in the message -- is not surprising for this simple example; we knew we were to be sent 16 bits and when we received the message we found out what each of the 16 bits was. |
H and
I might seem redundant because the formulas are similar. But
H does
not equal
I. Entropy refers to the uncertainty of an unknown message, and information refers to the probability of a known message occurring by chance alone.
| More accurately, entropy is a measure of uncertainty due to the unknown part of a message/particle system, and information is a measure of reduction of uncertainty due to the known part of a message/particle system. |
Information gained is equal to entropy lost. Information and entropy are two sides of the same probabilistic coin. While a flipped coin is spinning in the air the entropy
H is one bit (an unknown heads or tails), and the information
I is zero. When it lands and is observed, the entropy
H is zero, and the information
I is one bit (a known heads or tails).
S and
H (thermodynamic and Shannon entropy)
are equivalent, in that
S is directly proportional to
H, and this is because the same conditions hold for both systems.
S is reserved for thermodynamics, but
H can can be applied to any statistical system. As Shannon and Weaver wrote:
“...the quantity which uniquely meets the natural requirements that one sets up for ‘information’ ... turns out to be exactly that which is known in thermodynamics as entropy.”
The entropy S is a state function of a thermodynamic system, but it can't be directly measured like pressure and temperature (see measuring entropy). There is no entropy-meter; entropy must be infered by varying the state of a system near equlibrium and observing how other thermodynamic variables (pressure, temperature, etc.) respond. This is one reason why the statistical mechanics interpretation of entropy is so important:
"[The] ability to make macroscopic predictions based on microscopic properties is the main asset of statistical mechanics over
thermodynamics. Both theories are governed by the
second law of thermodynamics through the medium of
entropy. However, entropy in thermodynamics can only be known empirically, whereas in statistical mechanics, it is a function of the distribution of the system on its microstates." (from
statistical mechanics)
It might seem like this statistical interpretation of matter can cause matter to be "influenced" by our knowledge, or lack of knowledge, of its microstates. What does information or knowledge about microstates have to do with how a steam engine works! But this train of thought is a result of a misperception of microscopic states in nature. Which microstate a particle system is in is irreducibly (inherently) uncertain, in same sense that the position and momentum of individual particles are uncertain (Heisenberg's uncertainty principle). The fact that entropy almost always increases or stays the same (the second law of thermodynamics) is a statistical statement about the uncertainty of a particle system's microstate.
| The fact that entropy sometimes can and does decrease is often glossed over in discussions of, and even the statement of, the second law of thermodynamics. The usefulness of the second law (it's explanatory power) is due to how frequently entropy doesn't measureably increase for any large number of particles. For even small macroscopic systems with a small number of possible states (e.g. > 1,000 particles each with >10 possible states and >101,000 total possible states), it is highly improbable (p <<< 1/2) that a measureable increase of entropy (e.g. a fractional increase of 1/1,000) will occur in the (current) lifetime of the universe (~1010 years). Almost is good enough for physics too. |
James Clerk Maxwell's thought experiment
Maxwell's demon is an example of the importance of observability/uncertainty in discussing the second law. The experiment's resolution, that the demon
can't cheat the second law because she can't observe the microstate without altering it, highlights the importance of observability/uncertainty in physics.
[To Do: Show how these ideas can be extended to easily percieved messages, particulary images.]
53,754 bit (184 x 289) images ( 2
53754 possible images ). Each pixel is represented by one bit, black or white. These three are particular images, not arbitrary selections from all possible 184 x 289 one-bit images :
 Low algorithmic complexity "simple" (one of very few possible low information images) High "image available energy" (non-random intensity gradient) Farthest from equilibrium |
 Medium algorithmic complexity "complex" (one of a few possible medium information images) Medium "image available energy" Far from equilibrium |
 High algorithmic complexity "random" (one of many possible nearly random images) Low "image available energy" Near equilibrium |
眼看着就要“搬家”了。这几天需要整理一下。内部网络已经基本上调试好了。基本思路是,远程登录管理节点(windows的远程控制提供了一个很好的接口)。这样在校内就能通过管理节点控制内部网的服务器,进行计算了。将来办公室搬到新校区以后,不必每天都花2个多小时在路上,在家也可以干。剩下的就是是否能在老校区访问新校区IP的问题了——这是我无法控制的。
在调试过程中,也遇到了一些麻烦。由于一个不知道的原因,某些机器无法控制、而其他机器却可以。换了操作系统也没办法。幸亏最近由partner帮忙配的机器可以用。因为显卡配的是9600的,本来打算留在这里将来调试GPGPU用的,现在就搬过去做管理节点了。
2x4core服务器计算速度的问题好像是解决不了——一个2x4core的机器居然和2x2core的性能差不多——可能是CPU本身的性能问题。好在现在至少已经过了从无到有的阶段,将来配机器要谨慎。配完以后要做测试,积累经验,在硬件方面只懂个皮毛会浪费很多money。
接下来就是准备攻LBM。今年的自然基金开来是没戏了,不过确实也是基础太薄弱;即没有很强的空化方面的经验,LBM也是现学现卖。现在很多同行工作都很有进展,在这个行业混饭吃也不容易。LBM的问题也不少,昨天看了一篇文献,提到LBM在处理大密度比的问题时也有技术障碍,被打击了一下。不过还是需要试了再说。好歹发几篇文章,“东方不亮,西方亮”也行。这几天先看看文献,研究一下SC模型,做个算例测试一下。ESP实验则要等到开学,学生过来了再开展,争取在学报上发表一、两篇文章。
周围的人经常提醒注意劳逸结合,我想是对的。尽管自我感觉前一阶段的“高强度”工作没多大影响,但还是要注意。另外,抽空也要练练英语,时间长了有时候已经反应不过来了。上次老板过来,提到是否要给导游“tip”,居然没明白。丢了一下人。