Check Object Existence in TCL - tcl

I want to check that whether object exists or not in tcl.
I looked into info object options but didn't found something specific for object existence and info exists only works for variables not objects.
Any idea?
I created an object of struct::stack
::struct::stack aa
(Dcode) 52 % info object class aa
::struct::stack::stack_oo
It seems that it is in tcloo.
I think in Itcl find command works itcl::find object aa
But not aware of tcl_oo.

You probably just missed it: info object isa object is used to test whether a particular word refers to an object.
% info object isa object abcde
0
% oo::object create abcde
::abcde
% info object isa object abcde
1
% abcde destroy
% info object isa object abcde
0
Here, with some other commands…
% info object isa object oo::object
1
% info object isa object while
0
% info object isa object no.such.thing.at.all.ever
0

Related

Find the list of all duplicate vertices with tolerance in a large array in Fortran

I am trying to write an efficient subroutine in Fortran to find the list of all duplicate vertices in an array. The tolerance (fixed using parameter paraEps) corresponds to the radius around each point in which I look for the duplicate points. The vertices and defined for Finite Element Analysis and the range of each component will depend on the 3D geometry on which the methods will be applied (I can expect that values of the components could be varied in [-10^6,10^6] with a precision of 10^-6.
Currently the following code is running on around 150k vertices but it will be used on array of 1-3M vertices.
Example:
A array of vertices. Here I just use some small list of nodes. In my case, nodes are in a 3D space with no specific distribution.
verticesArray =
[ 0 0 0,
0 1 0
1 0 0
1 0 0
1 1 1
0 1 0
1 2 3
1 2 3
0 1.0001 0
0 1.1 0
1 2 4]
The result list (0 means the vertice has no duplicate) with a tolerance of 10^-3.
list = [ 0 1 2 2 0 1 3 3 1 0 0]
I have written a first version that works fine but remains slow especially the number of vertices is large.
Notice that startItem is an integer I can specify for starting the search not from the beginning of the array.
MWE:
module test
implicit none
contains
subroutine get_dup_nodes(nbNodes,dimP,arrayIn,nbDupNodes)
!
integer,intent(in) :: nbNodes,dimP ! number of nodes and dimension
double precision,dimension(:,:),intent(in) :: arrayIn ! list of coordinates
!
integer,intent(out) :: nbDupNodes
!
double precision,dimension(dimP) :: coorNode
integer,dimension(:),allocatable :: listDup !list of duplicates
logical :: currUnique
double precision,allocatable :: distVal
integer :: itN,itM,itF,minCurrIt,currIt,nbTmp,sizeR,startItemVal
!
double precision,parameter :: paraEps=1e-3
!
!initialize variables
allocate(listDup(nbNodes))
listDup=0
nbDupNodes=0
itF=0
distVal=1.
!
do itN=1,nbNodes
!coordinate current node
coorNode=arrayIn(itN,:)
currUnique=.true.
!search for current nodes coordinates in the list of nodes
if (listDup(itN).eq.0) then
do itM=itN+1,nbNodes
! compute distance to current point
distVal = NORM2(coorNode-arrayIn(itM,:))
! if the distance is to small then duplicate
if (distVal.le.paraEps) then
!first time it is a duplicate
if (currUnique) then
currUnique=.false.
nbDupNodes=nbDupNodes+1
listDup(itN)=nbDupNodes
endif
listDup(itM)=nbDupNodes
endif
enddo
endif
enddo
print *,listDup
end subroutine get_dup_nodes
end module test
program testb
use test
implicit none
double precision,dimension(33) :: valTmp
double precision,dimension(11,3) :: verticesArray
integer :: nbd
integer :: i,j,k
valTmp = (/ 0.,0.,0.,0.,1.,0.,1.,0.,0.,1.,0.,0.,1.,1.,1.,0.,1.,0.,1.,2.,3.,1.,2.,3.,0.,1.0001,0.,0.,1.1,0.,1.,2.,4. /)
k=1
do i=1,11
do j=1,3
verticesArray(i,j)=ValTmp(k)
k=k+1
enddo
print *,verticesArray(i,:)
enddo
call get_dup_nodes(11,3,verticesArray,nbd)
end program testb
Additional request: if you have references of books about such kind of algorithm, it could be useful.

Racket: eval, namespace-attach-module vs. namespace-require

Let's say I have a module "foo.rkt" which exports a struct foo, e.g.,
#lang racket (provide foo) (struct foo ())
In another module, I use "foo.rkt" but I'd also like to associate the binding to "struct foo" to another namespace (I don't use prefabs for various reasons, so I can't use namespace-require).
I thought that I could use namespace-attach-module as follows:
(define ns (make-base-namespace))
(namespace-attach-module (current-namespace) "foo.rkt" ns)
(eval '(foo) ns)
But that does not work, as namespace-mapped-symbols reveals that s is not bound in ns (if this is the only place to look for bindings). It does however work in the REPL. Why?
I assume the problem is to avoid instantiating the module in "foo.rkt" twice, since this leads to two incompatible structure definitions.
The function namespace-attach-module is part of the puzzle, but it only attaches
the instantiated module to the namespace ns - that is the name "foo.rkt" is now associated with the correct instantiation of "foo.rkt". It however doesn't make the bindings available in ns - that is the job of namespace-require.
Here is an example:
File: "computer.rkt"
#lang racket
(provide (struct-out computer))
(struct computer (name price) #:transparent)
File: "use-computer.rkt"
#lang racket
(require "computer.rkt") ; instatiate "computer.rkt"
(define ns (make-base-namespace))
(namespace-attach-module (current-namespace) "computer.rkt" ns) ; ns now knows the same instance
(define a-computer
(parameterize ([current-namespace ns])
(namespace-require "computer.rkt")
(eval '(computer "Apple" 2000) ns)))
(computer-name a-computer) ; works, since ns used the same instantiation of "computer.rkt"
The result of running this is:
"Apple"
Note that removing the namespace-attach-module line leads to the error:
computer-name: contract violation;
given value instantiates a different structure type with the same name
expected: computer?
given: (computer "Apple" 2000)
since without the attachment, the namespace-require will instatiate "computer.rkt" a second time leading to two incompatible structures begin declared.

How do I get result of invoke direct?

I am trying to understand what is happening in the following smali code:
I am trying to log the result or stored value in key:
# creates new instance of SecretKeySpec in register v8
new-instance v8, Ljavax/crypto/spec/SecretKeySpec;
# store contant 0x0 in v0
const/4 v0, 0x0
aget-object v0, v9, v0
# store string AES in v1
const-string v1, "AES"
# calls new SecretKeySpec(v0,v1);
invoke-direct {v8, v0, v1}, Ljavax/crypto/spec/SecretKeySpec;-><init>([BLjava/lang/String;)V
.line 115
.local v8, "key":Ljavax/crypto/spec/SecretKeySpec;
The invoke-direct call there is calling the constructor. Object creation in Java (and Dalvik) bytecode takes two instructions. The first, new-instance allocates an uninitialized object, while invoke-direct calls the constructor to initialize this object. The object is stored in v8, as you can see from the new-instance instruction.

Tcl API to get type of Tcl_obj*

Whether any Tcl Api exist which will tell the type of Tcl_Obj
whether it is string ,int or list
I want to check whether it is list so that i can put
{} over it
You're not supposed to do that. Tcl regards types as things that it is allowed to freely change behind the scenes, and makes no guarantee at all that what it is doing is what you expect.
That said, in Tcl 8.6 you can use tcl::unsupported::representation to get a description of a value that includes the current type.
% tcl::unsupported::representation [expr 1]
value is a int with a refcount of 1, object pointer at 0x100870c10, internal representation 0x1:0x0, no string representation
% tcl::unsupported::representation [expr 123456789123456789123456789]
value is a bignum with a refcount of 1, object pointer at 0x100871a20, internal representation 0x100882b90:0x20004, no string representation
% tcl::unsupported::representation [expr 1.5]
value is a double with a refcount of 1, object pointer at 0x1008713f0, internal representation 0x3ff8000000000000:0x100871420, no string representation
% tcl::unsupported::representation abc
value is a pure string with a refcount of 3, object pointer at 0x100874b10, string representation "abc"
% tcl::unsupported::representation [list a b c]
value is a list with a refcount of 3, object pointer at 0x100870e80, internal representation 0x100902f50:0x0, string representation "a b c"
% tcl::unsupported::representation [dict create a b c d]
value is a dict with a refcount of 3, object pointer at 0x1008717e0, internal representation 0x1008d4f10:0x0, string representation "a b c d"
% tcl::unsupported::representation [set s abc;string length $s;set s]
value is a string with a refcount of 5, object pointer at 0x100874b10, internal representation 0x1008a2ed0:0x1008710f0, string representation "abc"
% tcl::unsupported::representation {}
value is a bytecode with a refcount of 19, object pointer at 0x100870b50, internal representation 0x1008d2510:0x0, string representation ""
(That last one surprised me.)
% tcl::unsupported::representation [set c stdin;gets $c;set c]
value is a channel with a refcount of 5, object pointer at 0x100871810, internal representation 0x1008b9a10:0x100829a10, string representation "stdin"
At the C level, the types are in the nullable typePtr field of the Tcl_Obj structure. (The “pure string” example above has a null typePtr.) The typePtr points to a static Tcl_ObjType structure, which in turn has a name field that should have a human readable type name in it. The types themselves are not normally exposed to third-party code, though they might be possible to look up using Tcl_GetObjType(). Not all types are registered, by policy.
You should not make your code behave differently according to what type you receive. That is not the Tcl Way Of Doing Things. We really do mean this.

Cuda illegal memory access error when using array indexes stored in another array

I'm using cuda fortran and I've been struggling with this problem in one simple kernel and I couldn't find the solution.
Isn't it possible to use integer values stored in an array as the indexes for another array?
Here's a simple example (edited to include also the main program):
program test
use cudafor
integer:: ncell, i
integer, allocatable:: values(:)
integer, allocatable, device :: values_d(:)
ncell = 10
allocate(values(ncell), values_d(ncell))
do i=1,ncell
values(i) = i
enddo
values_d = values
call multipleindices_kernel<<< ncell/1024+1,1024 >>> (values_d,
+ ncell)
values = values_d
write (*,*) values
end program test
!////////////////////////////////////////////////////
attributes(global) subroutine multipleindices_kernel(valu, ncell)
use cudafor
implicit none
integer, value:: ncell ! ncell = 10
integer :: valu(ncell)
integer :: tempind(10)
integer:: i
tempind(1)=10
tempind(2)=3
tempind(3)=5
tempind(4)=7
tempind(5)=9
tempind(6)=2
tempind(7)=4
tempind(8)=6
tempind(9)=8
tempind(10)=1
i = (blockidx%x - 1 ) * blockdim%x + threadidx%x
if (i .LE. ncell) then
valu(tempind(i))= 1
endif
end subroutine
I understand that if there were repeated values in the tempind array different threads could be accessing the same memory location for reading or writting, but that is not the case.
Even though, this gives the error "0: copyout Memcpy (host=0x303610, dev=0x3e20000, size=40) FAILED: 77(an illegal memory access was encountered).
Does anyone know if it is possible to use this indexes coming from another array in cuda?
After some additional tests, I've noticed that the problem occurs not while running the kernel itself, but on the transfer of the data back to CPU (if I remove "values = values_d" then no error is displayed). Also, if I substitute in the kernel valu(tempind(i)) by valu(i) it works fine, but I want to have the indexes coming from an array since the purpose of this test is to make a parallelization of a CFD code where the indexes are stored like that.
The problem appears to be that the generated executable doesn't pass the variable ncell to the kernel correctly. Running the application through cuda-memcheck shows that threads outside of the 1-10 are passing through the branch statement, and adding a print statement to print ncell inside the kernel also gives strange answers.
It used to be a requirement that all attributes(global) subroutines had to reside within a module. This requirement seems to have been relaxed in more recent versions of CUDA Fortran (I cannot find references to it in the programming guide). I believe the code outside of the module is causing the error here. By placing multipleindices_kernel within a module and using that module in test I consistantly get correct answers with no errors. The code for this is below:
module testmod
contains
attributes(global) subroutine multipleindices_kernel(valu, ncell)
use cudafor
implicit none
integer, value:: ncell ! ncell = 10
integer :: valu(ncell)
integer :: tempind(10)
integer:: i
tempind(1)=10
tempind(2)=3
tempind(3)=5
tempind(4)=7
tempind(5)=9
tempind(6)=2
tempind(7)=4
tempind(8)=6
tempind(9)=8
tempind(10)=1
i = (blockidx%x - 1 ) * blockdim%x + threadidx%x
if (i .LE. ncell) then
valu(tempind(i))= 1
endif
end subroutine
end module testmod
program test
use cudafor
use testmod
integer:: ncell, i
integer, allocatable:: values(:)
integer, allocatable, device :: values_d(:)
ncell = 10
allocate(values(ncell), values_d(ncell))
do i=1,ncell
values(i) = i
enddo
values_d = values
call multipleindices_kernel<<< ncell/1024+1,1024 >>> (values_d, ncell)
values = values_d
write (*,*) values
end program test
!////////////////////////////////////////////////////