|
So were you able to solve the problem. It would be nice if you can share what type of code can cause optimization problems.
-Saurabh
|
|
|
|
|
Saurabh.Garg wrote: ForumVisual C++ / MFC
Subject:Re: Release Code Optimization Problems..
Sender:Saurabh.Garg
Date:22:36 21 May '06
So were you able to solve the problem. It would be nice if you can share what type of code can cause optimization problems.
-Saurabh
I'm still fighting with it. But at least, I know which files are the cause. It seems that the developer was not aware that optimizations could cause some aliasing..about aliasing. (Using pointers to variables that the compiler may supress during optimization)
Look in:
I will sumarize when I finish my investigation.
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
My articles
|
|
|
|
|
I hate those problems. There's almost nothing you can do to determine the problem. The only way I've been able to sort it out reliably is to narrow down to a particular section of code by comparing the results with what is expected every few lines, and flag an error (a message box or debug trace) when they don't match. When you can find out which section of code causes the problem, check it extremely carefully to catch things that might be dependent on the compiler moving statements around, and possibly rewrite the code. Even if it's slightly less efficient, the extra speed you get with optimisation will probably make it worthwhile.
Good luck
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Ryan Binns wrote: ForumVisual C++ / MFC
Subject:Re: Release Code Optimization Problems..
Sender:Ryan Binns
Date:7:53 20 May '06
I hate those problems. There's almost nothing you can do to determine the problem. The only way I've been able to sort it out reliably is to narrow down to a particular section of code by comparing the results with what is expected every few lines, and flag an error (a message box or debug trace) when they don't match. When you can find out which section of code causes the problem, check it extremely carefully to catch things that might be dependent on the compiler moving statements around, and possibly rewrite the code. Even if it's slightly less efficient, the extra speed you get with optimisation will probably make it worthwhile.
Yes, you have interpreted my question correctly. I guess that I will continue to dumping (via TRACE) debug information to catch the problem, it is like looking for a needle in a montain of hay. and even worse, the problem is detected only after running for almost 1hour. (That is the reason why speed optimizations are required)
Thanks for your help.
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
My articles
|
|
|
|
|
One other thing you could try is to enable optimisation for your debug build and see if it has the same problem. If it does, you can step through it with the debugger. You won't be able to go one line at a time, but you should be able to narrow it down enough to find the problem area.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Ryan Binns wrote: ForumVisual C++ / MFC
Subject:Re: Release Code Optimization Problems..
Sender:Ryan Binns
Date:20:38 20 May '06
One other thing you could try is to enable optimisation for your debug build and see if it has the same problem. If it does, you can step through it with the debugger. You won't be able to go one line at a time, but you should be able to narrow it down enough to find the problem area.
Ryan
I'm using your sugestion together with the sugestion in:
http://www.codeproject.com/script/comments/forums.asp?forumid=1647&select=1497199&fr=364&df=100#xx1497199xx[^]
So far, I managed to isolate a bunch of files that cause the problem, it seems that the developer that wrote that code was not aware about aliased code. (I asked him to take a look and maybe re-design some code that causes the problem.)
I will sumarize the results.
Thanks.
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
My articles
|
|
|
|
|
Sounds a lot like scientific programming.
I do not know your code, but this sort of problem often arises from:
1.)Array overrun (Solution: interface array access through an intermediary function that executes a bunch of assert(); to verify that access is always within bounds. The asserts all dissappear in the release mode so that they do not slow production down at all.)
2.)Optimizer bug. (Here the machine-code generated by the compiler is just flat wrong. Every mature scientific programmer must be prepared to deal with this horrible sort of problem. I have NEVER (since 1965) dealt with a compiler that I did not find bugs in its code generation. Here, my best approach is to start backward from the crash line. Either some datum going into the crash is wrong or the line is encoded wrong.)
Item 1.) is about 100 times more common than item 2.) but they lead to comparable amount of grief in toto.
Gematria
|
|
|
|
|
Ray Hagstrom wrote: ForumVisual C++ / MFC
Subject:Re: Release Code Optimization Problems..
Sender:Ray Hagstrom
Date:14:46 20 May '06
Sounds a lot like scientific programming.
I'm doing some system verification code, the system guys use Matlab for generating their models, and I run some "C" code to verify their models.
Ray Hagstrom wrote: 1.)Array overrun (Solution: interface array access through an intermediary function that executes a bunch of assert(); to verify that access is always within bounds. The asserts all dissappear in the release mode so that they do not slow production down at all.)
Yes, But this could also happen in the Debug or Release without speed optimization, and it does not happen, the problem arises only when I compile the code with speed optimization (/O2 flag) (PC-Lint[^] did not find any access out of bounds in the code)
Ray Hagstrom wrote: 2.)Optimizer bug. (Here the machine-code generated by the compiler is just flat wrong. Every mature scientific programmer must be prepared to deal with this horrible sort of problem. I have NEVER (since 1965) dealt with a compiler that I did not find bugs in its code generation. Here, my best approach is to start backward from the crash line. Either some datum going into the crash is wrong or the line is encoded wrong.)
The program does not crash, but produces wrong results when compiled with the optimize for speed flag. I'm currently adding some TRACE lines to print out the computed results and then compare them with the Debug (or Release without code optimization).. but that is like looking for a needle in a montain of hay
Any additional hint?
Thanks to all in advance.
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
My articles
|
|
|
|
|
Hello to all,
First of all, thanks for your help in trying to solve this problem.
The problem is solved, and was caused by variable aliasing that confused
the compiler optimizations.
The code that caused the problem has been re-designed and fixed. (I found also other spaguetti programming problems there).
How was this problem isolated
#pragma optimize( "", off )
...
#pragma optimize ("", on )
At the end, I found that only a small number of functions were the ones that caused the problem, and the reason was variable aliasing.
The code was belonging to a novice developer, that with my help he has re-designed his code and now we all are happy
Note: Variable aliasing is described in the VC documentation, look under Compiler Optimizations, specially for the flags /Ow or /Oa.
Thanks to all who helped me to identify the problem.
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
My articles
|
|
|
|
|
Is there any possibilities to convert my string to PUCHAR with out doing looping.
PUCHAR buffer = new UCHAR[length];
As well vice-versa case; meaning convert PUCHAR to String with out doing looping.
Please explain me.
|
|
|
|
|
sivaprakashshanmugam wrote: Is there any possibilities to convert my string to PUCHAR with out doing looping.
PUCHAR buffer = new UCHAR[length];
As well vice-versa case; meaning convert PUCHAR to String with out doing looping.
From PUCHAR to CString ...
CString csPUCHAR = buffer;
From CString to PUCHAR ...
PUCHAR puCS = (PUCHAR)csPUCHAR.GetBuffer(0);
csPUCHAR.ReleaseBuffer();
Nibu thomas
Software Developer
Programming Tips[^]
-- modified at 2:40 Saturday 20th May, 2006
|
|
|
|
|
Yes the first one works perfectly but it adds some junk character at the end how to remove that;
The second gives conversion error.
Error Message
error C2440: '=' : cannot convert 'ATL::CSimpleStringT<basetype,t_bmfcdll>::PXSTR' to 'PUCHAR'
with
[
BaseType=char,
t_bMFCDLL=true
]
Please provide me solution...
|
|
|
|
|
sivaprakashshanmugam wrote: Yes the first one works perfectly but it adds some junk character at the end how to remove that;
You should terminate the input string by a null char... '\0'
sivaprakashshanmugam wrote: The second gives conversion error.
Error Message
error C2440: '=' : cannot convert 'ATL::CSimpleStringT<basetype,t_bmfcdll>::PXSTR' to 'PUCHAR'
with
[
BaseType=char,
t_bMFCDLL=true
]
Cast it to PUCHAR .
Nibu thomas
Software Developer
Programming Tips[^]
|
|
|
|
|
Nibu thomas wrote: PUCHAR puCS = csPUCHAR.GetBuffer();
PUCHAR puCS = csPUCHAR.GetBuffer();
does this work ??
I think it should be
PUCHAR puCS = csPUCHAR.GetBuffer(csPUCHAR.GetLength());
Hope i am not wrong.
Appu..
"If you judge people, you have no time to love them."
|
|
|
|
|
NiceNaidu wrote: PUCHAR puCS = csPUCHAR.GetBuffer();
does this work ??
No. Sorry.
NiceNaidu wrote: I think it should be
PUCHAR puCS = csPUCHAR.GetBuffer(csPUCHAR.GetLength());
Hope i am not wrong
No you are not wrong... But no need for csPUCHAR.GetLength() , use 0 instead. We just need a pointer to the internal buffer of CString . I somehow missed out on that.
Nibu thomas
Software Developer
Programming Tips[^]
|
|
|
|
|
|
Hi,
I have registered mutiple document templates..in an MDI application.I have registered three document templates.When i run the application..a dialog is promting to select one out of the three, then the application main frame and child frame are loaded.I want..to disable this dialog initally when run application,i want to load only main frame.when i click new from menu i want this dialog to appear and prompt to select one out of the three registered templates .Is there any way to do this ..?
Thanks in before
James
|
|
|
|
|
Add the line in red to your code( exactly where it is shown here).
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
<code>
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;</code>
if (!ProcessShellCommand(cmdInfo))
return FALSE;
This is what the docs say about CCommandLineInfo::FileNothing ...
Turns off the display of a new MDI child window on startup. By design, AppWizard generated MDI applications display a new child window on startup. To turn off this feature, an application can use CCommandLineInfo::FileNothing as the shell command when calling ProcessShellCommand . ProcessShellCommand is called by the InitInstance( ) of all CWinApp derived classes.
Nibu thomas
Software Developer
Programming Tips[^]
|
|
|
|
|
|
RockyJames wrote: Thank you very much
Thanks for saying thanks...
Nibu thomas
Software Developer
Programming Tips[^]
|
|
|
|
|
How To Trap esc virtual key code for Dailog Based Application?
Fly Like An Eagle With MIGHTY POWER.
|
|
|
|
|
Use PreTranslateMessage()
BOOL MyDialog::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message==WM_KEYDOWN)
{
if(pMsg->wParam==VK_RETURN || pMsg->wParam==VK_ESCAPE)
pMsg->wParam=NULL ;
}
return CDialog::PreTranslateMessage(pMsg);
}
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
CodeVarma wrote: How To Trap esc virtual key code for Dailog Based Application?
See the following Links for more information.
How to use[^]
Where to use[^]
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
http://www.codeproject.com/script/comments/forums.asp?msg=1338807&forumid=1647&mode=all&userid=1817418#xx1338807xx
This question has been answered many times i suppose. Also read Michael Dunn's FAQ. It has a lot of good information in it.
Regards,
Rajesh R. Subramanian.
You have an apple and me too. We exchange those and We have an apple each.
You have an idea and me too. We exchange those and We have two ideas each.
|
|
|
|
|
//iocpsever.cpp
#include <winsock2.h>
#include "memorypool.h"
#include <mswsock.h>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "Mswsock.lib")
// 但I/O操作数据
typedef struct tagPER_IO_DATA
{
OVERLAPPED Overlapped;
WSABUF DataBuf;
char buffer[1024];
DWORD BufferLen;
int OperationType;
SOCKET Socket;
SOCKADDR_STORAGE ClientAddr;
}PER_IO_DATA, *LPPER_IO_DATA;
#define OP_READ 0
#define OP_WRITE 1
#define OP_ACCEPT 2
particle_allocator<per_io_data> g_per_io_data;
HANDLE g_hAcceptExOverEvent = NULL;
DWORD WINAPI ServerWorkerThread(LPVOID lpParam);
using namespace std;
int main(void)
{
WSADATA wsd;
SYSTEM_INFO SystemInfo;
SOCKADDR_IN InternetAddr;
SOCKET Listen;
WSAStartup(MAKEWORD(2, 2), &wsd);
HANDLE CompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE,
NULL,
0,
0);
GetSystemInfo(&SystemInfo);
for (DWORD i = 0; i < SystemInfo.dwNumberOfProcessors; ++i)
{
HANDLE ThreadHandle;
ThreadHandle = CreateThread(NULL,
0,
ServerWorkerThread,
CompletionPort,
0,
NULL);
CloseHandle(ThreadHandle);
}
Listen = WSASocket(AF_INET,
SOCK_STREAM,
0,
NULL,
0,
WSA_FLAG_OVERLAPPED);
InternetAddr.sin_family = PF_INET;
InternetAddr.sin_port = htons(10000);
InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(Listen, (SOCKADDR*)&InternetAddr, sizeof(InternetAddr));
listen(Listen, 5);
if (CreateIoCompletionPort((HANDLE) Listen, CompletionPort, (ULONG_PTR)&Listen, 0) == NULL)
{
printf("CreateIoCompletionPort failed with error %d\n", GetLastError());
return 1;
}
g_hAcceptExOverEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if(!g_hAcceptExOverEvent)
{
return 1;
}
if(WSAEventSelect(Listen, g_hAcceptExOverEvent, FD_ACCEPT) == SOCKET_ERROR)
{
return 1;
}
SetEvent(g_hAcceptExOverEvent);
BOOL b = TRUE;
while (b)
{
if(WaitForSingleObject(g_hAcceptExOverEvent, INFINITE) == WAIT_FAILED)
continue;
for(int i =0;i<10;i++)
{
PER_IO_DATA * pper_io_data = NULL;
DWORD dwAddrLen = sizeof(sockaddr_in)+16;
pper_io_data = (PER_IO_DATA *)g_per_io_data.alloc_particle();
pper_io_data ->Socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
pper_io_data->OperationType = 2;
ZeroMemory(&pper_io_data->Overlapped,sizeof(OVERLAPPED));
if(!AcceptEx(Listen,pper_io_data ->Socket,pper_io_data ->buffer,0,dwAddrLen,dwAddrLen,&pper_io_data ->BufferLen,&pper_io_data->Overlapped))
{
if(WSAGetLastError() != ERROR_IO_PENDING)
{
closesocket(pper_io_data->Socket);
g_per_io_data.free_particle((void*)pper_io_data); //归还结构体到内存池
continue;
}
}
setsockopt( pper_io_data ->Socket, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, ( char* )&( Listen ), sizeof( Listen ) );
}
}
CloseHandle(g_hAcceptExOverEvent);
return 0;
}
DWORD WINAPI ServerWorkerThread(LPVOID lpParam)
{
HANDLE CompletionPort = (HANDLE)lpParam;
DWORD BytesTransferred;
LPOVERLAPPED lpOverlapped;
LPPER_IO_DATA poldPerIoData = NULL;
LPPER_IO_DATA pnewPerIoData = NULL;
DWORD RecvBytes;
DWORD Flags;
BOOL bRet = FALSE;
while (1)
{
bRet = GetQueuedCompletionStatus(CompletionPort,
&BytesTransferred,
(PULONG_PTR)
&poldPerIoData,
(LPOVERLAPPED*)
&lpOverlapped,
INFINITE);
if(!bRet)
{
continue;
}
poldPerIoData = (LPPER_IO_DATA)CONTAINING_RECORD(lpOverlapped,
PER_IO_DATA,
Overlapped);
switch (poldPerIoData->OperationType)
{
case OP_ACCEPT:
CreateIoCompletionPort(
(HANDLE)poldPerIoData->Socket,
CompletionPort,
(ULONG_PTR)0,
0);
pnewPerIoData = (LPPER_IO_DATA)g_per_io_data.alloc_particle();
if (!pnewPerIoData)
{
// Error
}
pnewPerIoData->Socket = poldPerIoData->Socket;
pnewPerIoData->OperationType = OP_READ;
ZeroMemory(&(pnewPerIoData->Overlapped), sizeof(OVERLAPPED));
Flags = 0;
pnewPerIoData->DataBuf.len = 1024;
pnewPerIoData->DataBuf.buf = pnewPerIoData->buffer;
pnewPerIoData->OperationType = 0; // read
WSARecv(pnewPerIoData->Socket,
&(pnewPerIoData->DataBuf),
1,
&RecvBytes,
&Flags,
&(poldPerIoData->Overlapped),
NULL);
g_per_io_data.free_particle((void*)poldPerIoData);
break;
case OP_READ:
cout << poldPerIoData->DataBuf.buf << endl;
pnewPerIoData = (LPPER_IO_DATA)g_per_io_data.alloc_particle();
if (!pnewPerIoData)
{
// Error
}
pnewPerIoData->Socket = poldPerIoData->Socket;
pnewPerIoData->OperationType = OP_READ;
ZeroMemory(&(pnewPerIoData->Overlapped), sizeof(OVERLAPPED));
Flags = 0;
pnewPerIoData->DataBuf.len = 1024;
pnewPerIoData->DataBuf.buf = pnewPerIoData->buffer;
pnewPerIoData->OperationType = 0; // read
WSARecv(pnewPerIoData->Socket,
&(pnewPerIoData->DataBuf),
1,
&RecvBytes,
&Flags,
&(poldPerIoData->Overlapped),
NULL);
g_per_io_data.free_particle((void*)poldPerIoData);
break;
case OP_WRITE:
break;
}
}
}
//memorypool.h
#pragma once
#include <iostream>
#include <list>#include <windows.h>
//关键区锁
class CLock
{
CRITICAL_SECTION _crisection;
public:
CLock()
{
InitializeCriticalSection( &_crisection );
}
~CLock()
{
DeleteCriticalSection( &_crisection );
}
void lock()
{
EnterCriticalSection( &_crisection );
}
void unlock()
{
LeaveCriticalSection( &_crisection );
}
};
template <class particle="">
class particle_allocator
{
enum
{
chunk_size =1024
};
typedef unsigned char byte;
struct particle_list
{
byte *_data;
particle_list *_next;
};
std::list<byte *=""> _chunks;
particle_list* _free_list;
CLock _guard;
public:
particle_allocator()
{
_free_list = 0;
_used_particle_number = 0;
_free_particle_number = 0;
}
~particle_allocator()
{
for(std::list<byte *="">::iterator iter = _chunks.begin();iter!=_chunks.end();++iter)
{
delete [] (*iter);
}
while(_free_list)
{
particle_list *temp = _free_list;
_free_list = _free_list->_next;
delete temp;
}
}
particle* alloc_particle()
{
_guard.lock();
byte *momory;
particle_list* ¤t_list = _free_list;
if(!current_list)
{
momory = new byte[chunk_size*sizeof(particle)];
_chunks.push_front(momory);
for(int i =0;i< chunk_size;i++,_free_particle_number++)
{
particle_list *newnode = new particle_list;
newnode->_data = momory + i*sizeof(particle);
newnode->_next = current_list;
current_list = newnode;
}
}
byte *redata;
particle_list *de_node = current_list;
redata = _free_list->_data;
current_list = current_list->_next;
++_used_particle_number;
--_free_particle_number;
delete de_node;
_guard.unlock();
return new(redata) particle;
}
void free_particle(void *p)
{
byte *free_block = reinterpret_cast<byte *="">(p);
_guard.lock();
particle_list* ¤t_list = _free_list;
particle_list *newnode = new particle_list;
newnode->_data = free_block;
newnode->_next = current_list;
current_list = newnode;
--_used_particle_number;
++_free_particle_number;
_guard.unlock();
}
void getallocator()
{
using namespace std;
cout << "_used_particle_number is " << _used_particle_number << " _free_particle_number is "<< _free_particle_number << endl;
}
public:
size_t _used_particle_number;
size_t _free_particle_number;
};
01 is program
|
|
|
|
|