Can I check whether a pointer's context was destroyed/disactivated? - memory-management

If you try to cuMemFree() a pointer, allocated in some device's primary context, after that context has been deactivated (= destroyed), it seems you get a CUDA_INVALID_VALUE error.
I need to differentiate this situation (in which it's fine to just forget about the piointer) to the situation of just passing a junk value, a "really" invalid value, to cuMemFree()?
Obviously, I could keep an indication of which context the allocation happened in, and whether it was primary or not. But - this is cumbersome and I would rather avoid it. Could I check something about the pointer itself to differentiate the two cases?

Related

How can I tell whether it's safe/necessary to cudaFree() or not?

I've allocated some GPU global memory with cudaMalloc(), say, in the constructor of some class. Now it's time to destruct the instance I've constructed, and I have my instance's data pointer. The thing is, I'm worried maybe some mischievous code elsewhere has called cudaDeviceReset(), after which my cudaFree() will probably fail (I'll get an invalid device pointer error). So, how can can I tell whether my pointer is elligible for cudaFree()ing?
I don't believe you can do much about that.
About the best you can do is try and engineer the lifespan of objects which will call the CUDA APIs in their destructors to do so before context destruction. In practice, that means having them fall of of scope in a well defined fashion before the context is automatically or manually torn down.
For a call like cudaFree(), which is somewhat "fire and forget" anyway, the best thing to do might be to write your own wrapper for the call and explicitly catch and tastefully ignore any obvious error conditions which would arise if the call was made after context destruction-
Given what talonmies says, one might consider doing the converse:
wrap your cudaDeviceReset() calls to also regard a 'generation counter'.
Counter increases will be protected by a lock.
While you lock, you reset and increment the generation counter.
Wrap cudaMalloc() to also keep the generation index (you might need a class/struct for that) - obtained during allocation (which also locks).
Wrap cudaFree() to lock and only really cudaFree() if the reset generation has not changed.
... now, you might say "Is all that locking worth it? At worst, you'll get an error, it's not such a big deal." And, to be honest - I'm not sure it's worth it. You could make this somewhat less painful by using a Reader-Writer lock instead of a simple lock, where the allocate and free are just readers that can all access concurrently.

Why does "garbage" data appear to not be meaningful?

I have always wondered why garbage data appears to not be meaningful. For clarity, what I mean by "garbage" is data that is just whatever happens to be at a particular memory address, that you have access to because of something like forgetting to initialize a variable.
For example, printing out an unused array gave me this:
#°õN)0ÿÿl¯ÿ¯ÿ ``¯ÿ¯ÿ #`¯ÿø+))0 wy¿[d
Obviously, this is useless for my application, but it also seems like it is not anything useful for any application. Why is this? Is there some sort of data protection going on here perhaps?
As you state in your question:
... "garbage" is data that is just whatever happens to be at a particular memory address, that you have access to because of something like forgetting to initialize a variable.
This implies that something else used to be in that memory before you got to use it for your variable. Whatever used to be there may or may not have any relation to how you wish to use the variable. That is, most languages do not force memory used for one type of object to be reused for the exact same type.
This means, if memory was used to store a pointer, and then released, that same memory may be used to store a string. If the pointer value was read out as if it was a string, something that looks like garbage may appear. This is because the bytes used to represent a pointer value are not restricted to the values that correspond to printable ASCII values.
A common way to detect a buffer overrun has occurred in a program is to examine a pointer value and see if it contains printable ASCII values. In this case, the user of the memory as a pointer sees junk, but in this case it is "printable".
Of course memory is never garbage, unless you make a conscious effort. After all, you are on a deterministic machine, even if it doesn't always seem like it. (Of course, if you interprete arbitrary bytes as text then it's unlikely that you see yourself as ASCII art, although you would deserve it.)
That was the reason for one of the worst bugs in history, quite recently, cf. https://xkcd.com/1354/. Where do you live to have missed it?

Is 'handle' synonymous to pointer in WinAPI?

I've been reading some books on windows programming in C++ lately, and I have had some confusing understanding of some of the recurring concepts in WinAPI. For example, there are tons of data types that start with the handle keyword'H', are these supposed to be used like pointers? But then there are other data types that start with the pointer keyword 'P'. So I guess not. Then what is it exactly? And why were pointers to some data types given separate data types in the first place? For example, PCHAR could have easily designed to be CHAR*?
Handles used to be pointers in early versions of Windows but are not anymore. Think of them as a "cookie", a unique value that allows Windows to find back a resource that was allocated earlier. Like CreateFile() returns a new handle, you later use it in SetFilePointer() and ReadFile() to read data from that same file. And CloseHandle() to clean up the internal data structure, closing the file as well. Which is the general pattern, one api function to create the resource, one or more to use it and one to destroy it.
Yes, the types that start with P are pointer types. And yes, they are superfluous, it works just as well if you use the * yourself. Not actually sure why C programmers like to declare them, I personally think it reduces code readability and I always avoid them. But do note the compound types, like LPCWSTR, a "long pointer to a constant wide string". The L doesn't mean anything anymore, that dates back to the 16-bit version of Windows. But pointer, const and wide are important. I do use that typedef, not doing so will risk future portability problems. Which is the core reason these typedefs exist.
A handle is the same as a pointer only so far as both ID a particular item. Obviously a pointer is the address of the item so if you know it's structure you can start getting fields in the item. A handle may or may not be a pointer - basically if it is a pointer you don't know what it is pointing to so you can't get into the fields.
Best way to think of a handle is that it is a unique ID for something in the system. When you pass it to something in the system the system will know what to cast it to (if it is a pointer) or how to treat it (if it is just some id or index).

Windows Azure TableServiceContext UndoChanges

When trying to save an object using an illegal character (e.g. '/') in the RowKey an exception is thrown when calling
_tableServiceContext.AddObject(tableName, myEntity);
I get an System.Data.Services.Client.DataServiceRequestException with the info "out of range", which is easily prevented (by preventing or checking for illegal characters).
My question now is, how do I recover from this status? After such an error all succeeding AddObject calls fail, presumably because the "corrupt object" still belong to the context.
Therefore I'm looking for a "UndoChanges" possibility.
Any hints are appreciated, thanks.
P.S.:
I'm looking for a better way than to throw away the context or to delete the object from the context...
The best way is probably to detach the entity from the context. If that's not an option for some reason, you could retry the save with SaveChangesOption.ContinueOnErrors set. eg,
_tableServiceContext.SaveChangesWithRetries(SaveChangesOptions.ContinueOnError);
This basically applies all the updates one at a time, instead of in batches. Note it will ignore all errors that occur, not just the one from the illegal entity.

What point should you cache a value for a Property?

I've always wondered about when and where is the best time to cache a property value... Some of them seem pretty simple, like the one below...
public DateTime FirstRequest {
get {
if (this.m_FirstRequest == null) {
this.m_FirstRequest = DateTime.Now;
}
return (DateTime)this.m_FirstRequest;
}
}
private DateTime? m_FirstRequest;
But what about some more complicated situations?
A value that comes from a database, but remains true after it's been selected.
A value that is stored in a built-in cache and might expire from time to time.
A value that has to be calculated first?
A value that requires some time to initialize. 0.001s, 0.1s, 1s, 5s???
A value that is set, but something else may come and set it to null to flag that it should be repopulated.
??? There seems to be limitless situations.
What do you think is the point that a property can no longer take care of itself and instead require something to populate its value?
[EDIT]
I see suggestions that I'm optimizing too early, etc. But my question is for when it is time to optimize. Caching everything isn't what I'm asking, but when it is time to cache, whose responsibility should it be?
In general, you should get the code working first and then optimize later and then only do optimizations that profiling say will help you.
I think you need to turn your question the other way around lest you fall into a trap of optimizing too early.
When do you think is the point that a property no longer needs recalculating on every call and instead uses some form of caching?
The caching of a value is an optimization and should therefore not be done as the norm. There are some cases where it is clearly relevant to use this optimization but in most cases, you should implement the property to work correctly every time by doing the appropriate work to get the value and then look to optimize it once you've profiled and shown that optimization is required.
Some reasons why caching is or is not a good idea:
- Don't cache if the value is prone to frequent change
- Do cache if it never changes and you are responsible for it never changing
- Don't cache if you are not responsible for providing the value as you're then relying on someone else's implementation
There are many other reasons for and against caching a value but there is certainly no hard and fast rule on when to cache and when not to cache - each case is different.
If you have to cache...
Assuming that you've determined some form of caching is the way to go, then how you perform that caching depends on what you are caching, why, and how the value is provided to you.
For example, if it is a singleton object or a timestamp as in your example, a simple "is it set?" condition that sets the value once is a valid approach (that or create the instance during construction). However, if it's hitting a database and the database tells you when it changes, you could cache the value based on a dirty flag that gets dirtied whenever the database value says it has changed. Of course, if you have no notification of changes, then you may have to either refresh the value on every call, or introduce a minimum wait time between retrievals (accepting that the value may not always be exact, of course).
Whatever the situation, you should always consider the pros and cons of each approach and consider the scenarios under which the value is referenced. Do the consumers always need the most recent value or can they cope with being slightly behind? Are you in control of the value's source? Does the source provide notification of changes (or can you make it provide such notifications)? What is the reliability of the source? There are many factors that can affect your approach.
Considering the scenarios you gave...
Again, assuming that caching is needed.
A value that comes from a database, but remains true after it's been selected.
If the database is guaranteed to retain that behavior, you can just poll the value the first time it is requested and cache it thereafter. If you can't guarantee the behavior of the database, you may want to be more careful.
A value that is stored in a built-in cache and might expire from time to time.
I would use a dirty flag approach where the cache is marked dirty from time to time to indicate that the cached value needs refreshing, assuming that you know when "time to time" is. If you don't, consider a timer that indicates the cache is dirty at regular intervals.
A value that has to be calculated first?
I would judge this based on the value. Even if you think caching is needed, the compiler may have optimised it already. However, assuming caching is needed, if the calculation is lengthy, it might be better during construction or via an interface such as ISupportInitialize.
A value that requires some time to initialize. 0.001s, 0.1s, 1s, 5s???
I'd have the property do no calculation for this and implement an event that indicates when the value changes (this is a useful approach in any situation where the value might change). Once the initialization is complete, the event fires, allowing consumers to get the value. You should also consider that this might not be suited for a property; instead, consider an asynchronous approach such as a method with a callback.
A value that is set, but something else may come and set it to null to flag that it should be repopulated.
This is just a special case of the dirty flag approach discussed in point 2.

Resources