Creating an initialization file in pari-gp - c

This question is definitely a stupid question. But, coming from C; I'm having trouble adding header files or "an initialization" file to my pari-gp code. This is to mean; I have a 1hr compile of code to make one vector; and I can use that vector once initialized; but I want to make a file of this vector such that I can access it once it's compiled once.
Here's the code without a header file; which takes about an hour to compile (given the series precision/numerical precision which are set to 100).
\p 100
\ps 100
Phi_Inv(w,l,{n=100}) =
{
my(out = 0);
for(i=0,n,
out = w*exp(out)/(exp(l*(n+1-i))+w)
);
out;
}
beta_init(n) = {
beta_taylor = vector(100,i,polcoef(Phi_Inv(w,l,n),i-1,w));
print(beta_taylor);
}
Rather than the brutal assignment of beta_taylor; and the caveman like print(beta_taylor), how can I write this to an initialization file I can package with the script. That is; an X mb file with all the coefficients neatly packed together. And if the file is lost, just run the code (which will take an hour) to write the initialization file again.
I mean, how would I properly do #include test.h where test.h is just a very long list of Taylor series values. So that I can just include this file, and write beta_taylor[i] for the i'th function. Such that it's as simple as including variables, like in C. I know I'm missing something simple and it's frustrating--making me feel stupid.
I'm mostly just asking about the syntax to go about and do this. I think I know how; but I imagine it's not the best way.
Any help or suggestions are greatly (And I really mean it, Greatly) appreciated.
To make a long story short; how do I save beta_taylor as a file we load when we initialize the program, and if the file is deleted we can save the program again by running the code for an hour?
Regards

So you want to serialize your vector of numbers to a file and read it back in later?
writebin() to the rescue. Something like
beta_init(n) = {
beta_taylor = vector(100,i,polcoef(Phi_Inv(w,l,n),i-1,w));
writebin("beta_taylor.dat", beta_taylor);
}
Run the function in one gp session, and then in another session, beta_taylor=read("beta_taylor.dat").
Compiling your code first with gp2c before running it to calculate the numbers will speed things up if you're not already doing that, btw. gp2c-run makes it easy by compiling a file and starting a new gp session with the resulting shared library already loaded. You might also look into if the parallel operations can be used here to speed up the initial computation; reading the documentation for parvector() I don't think it can be, though, because of that mysterious l variables used in beta_init() that I don't see you define anywhere, but you might be able to re-phrase your equation with hardcoded constants or something.

An initialization for Pari/GP, at program start:
(file gprc.txt in directory of gp.exe)
lines = 25
colors = "brightfg"
histfile = "gp_history.txt"
breakloop = 0
help = "# perl\\perl gphelp.pl -detex -ch 10 -cb 11 -cu 12"
prompt = "gp >"
prompt_cont="gpc>"
datadir = "u://paritty/syntax/_v11/"
path = "u://paritty/syntax/_v11/"
primelimit = 100 000 000
parisizemax = 1 000 000 000
read "__init.gp"
echo = 0
The file __init.gp contains my generally used functions; commands to read precomputed data-vectors can of course be included there. If no path is indicated it will be searched in the directory given in the path= statement.

Related

Renaming & moving my file based on the size is not always working in c. Why?

I have an application, written in C, which generates various data parameters that I am logging into a text file named debug_log.txt. Whenever this log file reaches 1 MB, I am renaming the filename with timestamp ex debug_log_20200106_133000.txt & moving it in same directory. I am then reopening debug_log.txt to log new parameters.
if(stat("/home/log/debug_log.txt", &statFiledbg) == 0)
{
if(statFiledbg.st_size >= 1048576) // 1MB
{
current_time = time(0);
strftime(time_buffer, sizeof(time_buffer), "%Y%m%d_%H-%M-%S", gmtime(&current_time));
sprintf(strSysCmddbg, "mv /home/log/debug_log.txt /home/log/debug_log%s.txt", time_buffer);
system(strSysCmddbg);
fp_dbglog = freopen("/home/log/debug_log.txt", "w", fp_dbglog);
}
}
The code works most of the time until it doesn't. After running the application for couple days, I see that debug_log.txt grows beyond 1 MB while the last moved & renamed log file is empty.
What could be the reason?
Use the rename function from the C standard library (in stdio.h) and check errno if it failed to know the exact reason why it is failing.
When working with files, and I/O in general, there are many, many things that can go wrong.
One of my senior developer in the company told me so. Is there anything wrong with using system()?
Yes: it is unnecessary (C and POSIX provide you with a function for basic usages like this), nonportable (it assumes you are in a system that has a "mv"), slower (it needs to spawn another process) and wrong for many use cases (eg. here there is no way to know what exactly failed unless you save the textual output of mv).
See questions and answers like Moving a file on Linux in C for an in-depth explanation.

tcl "open" command not working when replacing Tcl_Filesystem with a duplicate

I'm trying to write a custom filesystem for Tcl using the Tclapi (it's work related, won't go into details), but I'm stuck trying to figure out why this is not working.
In this code segment I'm getting the original/native Tcl_Filesystem, copying over all its contents (function pointers) to my_fs, and then calling Tcl_FSRegister on my_fs. Very simple, thought it should work.
// global scope
const Tcl_Filesystem *ori_fs;
Tcl_Filesystem *my_fs;
...
// in Init
// Get the original Tcl_Filesystem.
Tcl_Obj *root_obj = Tcl_NewStringObj("/", -1);
Tcl_IncrRefCount(root_obj);
ori_fs = Tcl_FSGetFileSystemForPath(root_obj);
Tcl_DecrRefCount(root_obj);
// create a duplicate of the original Tcl_Filesystem struct.
my_fs = malloc(sizeof(Tcl_Filesystem));
memmove(my_fs, ori_fs, ori_fs->structureLength);
int ret = Tcl_FSRegister((ClientData)1, my_fs);
if (ret == TCL_ERROR) {
...
When I ran
load <path to .so>/my_fs[info sharedlibextension]
# sanity check
puts [pwd]
set fp [open test.txt]
however, I get this
<my current directory>
while executing
"open test.txt"
invoked from within
"set fp [open test.txt]"
(file "test.tcl" line 3)
Notice how "puts [pwd]" works but not "open test.txt" ?
Replacing "my_fs" with "ori_fs" in the call to Tcl_FSRegister seems to work...
I've already spent far too much time trying to figure this out. I would appreciate if anyone could help me with this!
The native filesystem is special. In particular, there's some places where its identity is used directly: for example, it's the only FS that can have temporary files made on it, it's assumed to own the roots, and it is handled specially in path management. (Well, according to where in the source code there are direct references to the Tcl internal variable tclNativeFilesystem, which isn't something you can cheat at. It's also possibly in read-only memory, so you can't hack around this.)
For most sane uses of a Tcl virtual filesystem, this doesn't matter. Temp files have to be native because you may well be passing them to the OS (e.g., for loading libraries or running programs that were inside the VFS; with these, they have to be copied out or the OS will think “what are you talking about?!”) and you put the things that you are mounting somewhere other than the native root. So long as you're not trying to use a VFS as a security measure (not recommended; there are safe interpreters for that as they offer a stronger sandboxing solution) it shouldn't be a problem as you can just make your code know that it needs to work below a particular location to get things done. (FWIW, it's a bad idea to cd anyway, except in response to user requests, since it changes the meaning of user-supplied relative paths, so good code handles “make everything relative to a defined location” from the start.)

Get a file's creation time with GLib

it can't be that hard but I just don't get it: I want to retrieve a file's creation time (not modification time) from within GLib (I am using Vala at the moment, but C or any other language with GLib Binding would do). I have this code:
File file = File.new_for_commandline_arg(args[1]);
FileInfo info = file.query_info("*", 0);
uint64 t = info.get_attribute_uint64(FileAttribute.TIME_CREATED);
stdout.printf("%llu\n", t);
which prints out 0 (indicating "invalid" according to the docs) on any file. I know there is a info.get_modification_date() available, which works as expected, but I need the time of the file's creation.
Google did not give me any results, so I hope someone here could give me a clue in the right direction.
Thanks a lot in advance!
If you are running your code on Linux, it's simply because that information is not available. You can check with g_file_info_has_attribute if a given attribute is actually available for a GFileInfo.

Windbg stacktrace shows line numbers before or after macro expansion

I have a C file with several macros.
The exe generated from the file crashes several times reporting events in the Windows event viewer. Upon taking a dump of the process and analyzing it using WinDbg with the correct pdb files for the symbols, we get the stacktrace and know the function which is causing the problem.
The stacktrace shows the line number of our function code which called other functions one of which led to the crash-
08 msvcr80!fwrite(void * buffer = 0x00000000`01ded180, unsigned int64 size =
0x1fff38, unsigned int64 count = 0x524fe123, struct _iobuf * stream =
0x00000000`00000000)+0x5f [f:\dd\vctools\crt_bld\self_64_amd64\crt\src\fwrite.c
# 77]
09 <function name>(void * param = 0x00000000`02d15a00)+0xb02
[<path to file> # 1516]
Our function called fwrite, which is shown to be at line 1516. However, there is no call to fwrite at 1516. (The crash happens because the stream argument to fwrite is 0x0)
I was wondering if these line numbers correspond to the source file after the macros are expanded ? What could be the reason for a possibly wrong line number ?
EDIT : The exe here is a debug build and was compiled with optimizations disabled.
I loaded the dump again in WinDbg but also linked in the source file to WinDbg itself this time. It points to line 1516 and upon viewing that in the source from WinDbg, it points to a line where there is no call to fwrite. However, there is such a call a few lines above.
Well i do not have direct answer to question here :(
But i would make a COD file. a file that maps source code to assembly code. Then see the assembly code generated for the function of interest. Specifically related to line 1516.
Am hoping that would give a fair insight as to whats going on behind the scene. You may want to give a quick try.
You just need to turn on a compiler flag to generate COD file. More can be read here

Debugging in c for log file

I have written down a program in c and I am trying to create a log file of it.
The problem I am facing is that while printing the outputs of each line in the file I want to write some distinctive feature such as the time of execution of that line or even the line number in the code.
Is there any way I can get to know any of these two.
I don't mind if you suggest some other way to get a distinctive feature. All I want is that looking at the log file the user gets to know that a certain part of the code was getting executed.
Thanks
I am working on linux and thus using the GCC compiler....
I have made a header file and in it I am for testing purposes writing __LINE__ . What I want to do is that in a program when I include this function of header file the line number gets printed where the function is. But instead i get the line number of the header file printf statement.
What do I need to do to get the line number of the file .
This is just a test format given below :-
new.h
void print()
{
printf("Line number is %d",__LINE__);
}
actual file
#include "new.h"
int main()
{
print();
}
Then I want that the line number that should be printed is that of actual file and not new.h which happens now....
Most C compilers provide some macros to identify each line, function, etc. With GCC, for example, you can use __LINE__, __FUNCTION__, and so on. Check your compiler documentation for details. To get a timestamp, you'll need to let us know what system you're working on.
If you want the actual date and time the function was executed, try asctime(). There is a good reference on how it's done here.
This will output something akin to Sat May 20 17:36:17 2000. If you want the time in seconds since the program started, have a variable such as int startTime = time() which holds the program start time in seconds from the Unix Epoch. Then, simply print startTime - time() to get the number of seconds since program start.
In GCC you can get line number as "__LINE__". Filename - "__FILE__".
If you want calculate execution time then just remember time on start, get time on end and substract them.
The line number can be obtained by the preprocessor macro __LINE__. The file is __FILE__. As for time, use the relevant OS library.
Or, use a logging library that support these.
Use __FILE__ and __LINE__ to get the current file and line number.
Edit: based on your edited question. Here's a simple way to do it to start.
new.h
#define PRINT() print(__LINE__)
void print(int line)
{
printf("Line number is %d",line);
}
actual file
#include "new.h"
int main()
{
PRINT();
}

Resources