Skip to content

Conversation

lan496
Copy link
Member

@lan496 lan496 commented Jun 10, 2023

This PR addresses the following as a by-product of my code reading for the layer group implementation.

  • delete unused functions
  • add docstrings and comments
  • refactor match_hall_symbol_db
  • add tests for delaynay reduction and layer-group symmetry

@lan496 lan496 added enhancement Significant ehancements layer-group labels Jun 10, 2023
@lan496 lan496 requested review from atztogo and LecrisUT June 10, 2023 00:30
@lan496
Copy link
Member Author

lan496 commented Jun 10, 2023

So, we need to merge #278 first to fix CI's clang-format version...

@lan496 lan496 marked this pull request as draft June 10, 2023 00:41
@lan496 lan496 changed the title Initial refactor for layer group implementation [Blocked by #278]Initial refactor for layer group implementation Jun 10, 2023
Copy link
Collaborator

@atztogo atztogo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lan496, great. Thanks!

@lan496 lan496 removed the request for review from LecrisUT June 10, 2023 05:24
#include <gtest/gtest.h>

extern "C" {
#include <math.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use extern "C" on std libraries. Let the cpp compiler optimize itself.

sym_free_symmetry(symmetry);
cel_free_cell(cell);
symmetry = NULL;
cell = NULL;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NULL -> nullptr. Also you mean to assert it not assign it here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least for me, I am not familiar with C++. If we want to write in C, is there any good way to do it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that writing in C++, especially for these tests it is more advantageous because:

  • you get a clear trace of how/what fails
  • you decouple potential C memory issues, e.g. not pre-allocating pointers from tests result errors

In these case I only have 2 simple suggestions:

  • Use std::array and continue to use the C interface as you would.
    The reason for this is that you allow in the future to use a wider range of built-in tools like range-for loops similar to python, that make the code much easier to read and detangle.
  • Check with the IDE's suggestion.

The NULL -> nullptr is suggested by CLion and that is because the NULL definition in C is different then C++'s nullptr and C++ uses the latter for enforcing that a pointer is truely null and not a pointer assigned to (void*)0. Think of the issues of comparing addresses of empty strings (&"" == &""). That is not guaranteed to give true and depends on the compiler's optimization.

Other than that there is a genuine error here in that it is supposed to be ASSERT_EQ(symmetry == nullptr) and not an assignment symmetry = nullptr.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point. But I want to confine this PR about the algorithmic side for the layer-group implementation for now. After we adopt C11 in develop branch, the corresponding refactoring would be awesome.

}

TEST(test_delaunay, test_delaunay_reduce_layer) {
double min_lattice[3][3];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm always paranoid with C code. Fo you need to explicitly deallocate these? I prefer to use std::vector to be safe and pass c_pointer to C functions and use reinterpret_cast when needed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you comment in general? Or some specific point?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both. In this case with fixed sized arrays like that, you don't need to use de-allocator?

And in general I am paranoid. E.g. if there is thread_local/static pointer and if for example someone uses the python api and the pointer is replaced without proper de-allocation, then that's a sure way to get memory leaks. If we impose and fix the issues specified by the compiler warnings in -Wall, clang-tidy inspection, and use the necessary sanitizations, then I would be less paranoid about such hypothetical issues.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point. It is uneasy to take a balance of feature developments and fixing holes. I want to merge this PR without going far from the original aim of PR.

Copy link
Collaborator

@LecrisUT LecrisUT Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, but at the very least new code should not introduce new holes. Whenever you are touching a part of a code that your IDE/clang-tidy and any other tools tells you to fix, then do a quick fix for that, otherwise the holes are never addressed.

See for example the warnings already displayed by github in the Files changed tab. (Amazed that github is able to do that 😮)

Copy link
Collaborator

@atztogo atztogo Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can invalid use of pointers to arrays with different qualifiers in ISO C before C2X [-Wpedantic] and pointers to arrays with different qualifiers are incompatible in ISO C [-Wpedantic]
be postponed to fix? Or just removing const can be a solution?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a bit of experimentation and research and tl;dr: we don't need to address "invalid * with different qualifiers".
There aren't concerning warnings in this PR now and I did a clang-tidy test

$ pre-commit run --hook-stage manual clang-tidy --from-ref origin/develop --to-ref lan496/layer-group-refactor
clang-tidy...............................................................Passed

All seem well for this PR at least on this front. There are still the conversations that I have not marked as resolved which should be addressed and I'm all 👍 for now.

Actually concerning warnings

The actual concerning warnings are not in this

src/refinement.c:1451:9: warning: 'mat_multiply_matrix_di3' accessing 72 bytes in a region of size 24
src/spacegroup.c:1150:51: warning: 'j' may be used uninitialized
About "invalid * with different qualifiers"

According to the wording in the C standard pre C23, it is interpreted that:

const double matrix[2][3]

which is a pointer to a (double-typed) pointer has the const such that it is a pointer to a (double-typed) const pointer, instead of the more natural pointer to a (const double-typed) pointer, meaning that in the former you cannot change where a slice is pointing to and in the latter you cannot change the values of the matrix array.

But in reality no compiler is following the logic of the former and instead they all follow the latter which is consistent with C++ standard as well. And in C23 they have changed the wording so that it is correctly interpreted as the latter (values are constant, and not pointers to the array slice is constant). Indeed if you set CMAKE_C_STANDARD=23 these warnings go away.

Here is a more detailed example of my tests:

#include <stdio.h>

void test_const_matrix(const int matrix[][2]){
    int vector[2] = {2,3};
//    matrix[0][0] = 2;
    matrix = vector;
    matrix = &vector;
}
void test_const_vector(const int vector[2]){
//    vector[0] = 2;
}
void test_matrix(int matrix[][2]){
    int vector[2] = {2,3};
    matrix[0][0] = 2;
    matrix = vector;
    matrix = &vector;
}
void test_vector(int vector[2]){
    vector[0] = 2;
}

int main() {
    int t_matrix[2][2] = {0};
    int t_vector[2] = {0};
    test_const_matrix(t_matrix);
    test_const_vector(t_vector);
    test_matrix(t_matrix);
    test_vector(t_vector);
    printf("Hello, World!\n");
    return 0;
}

Here test_const_matrix would fail as expected with matrix[0][0]=2 even though the wording would indicate otherwise even when I bump down the standard. Although I did not test with antiquated gcc compilers to confirm this behaviour is true in old compilers as well. (Note that in the example given there are additional warnings about invalid operations that should be considered).

I'll leave this conversation unresolved for now, just for visibility.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @LecrisUT.

For warning: 'mat_multiply_matrix_di3' accessing 72 bytes in a region of size 24, I have really no idea.

For warning: 'j' may be used uninitialized, this is OK, which is confirmed by assert.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For warning: 'j' may be used uninitialized, this is OK, which is confirmed by assert.

Not really, I will explain in a PR where I address the clang-tidy issues.

@lan496 lan496 changed the title [Blocked by #278]Initial refactor for layer group implementation Initial refactor for layer group implementation Jun 10, 2023
@codecov-commenter
Copy link

codecov-commenter commented Jun 15, 2023

Codecov Report

Patch coverage: 82.69% and project coverage change: +0.16 🎉

Comparison is base (3800c46) 85.92% compared to head (724e786) 86.09%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #288      +/-   ##
===========================================
+ Coverage    85.92%   86.09%   +0.16%     
===========================================
  Files           23       23              
  Lines         6082     6069      -13     
===========================================
- Hits          5226     5225       -1     
+ Misses         856      844      -12     
Flag Coverage Δ
c_api 76.50% <80.76%> (+2.28%) ⬆️
fortran_api 37.46% <17.30%> (+0.08%) ⬆️
python_api 82.83% <82.69%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/kpoint.c 97.81% <ø> (ø)
src/pointgroup.c 76.79% <66.66%> (-0.16%) ⬇️
src/spacegroup.c 91.42% <80.76%> (-0.24%) ⬇️
src/delaunay.c 96.11% <100.00%> (+6.98%) ⬆️
src/niggli.c 85.12% <100.00%> (-0.16%) ⬇️
src/spglib.c 73.00% <100.00%> (-0.04%) ⬇️
src/symmetry.c 80.70% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@lan496 lan496 force-pushed the layer-group-refactor branch from db667c9 to 1de4f12 Compare June 17, 2023 10:12
@atztogo
Copy link
Collaborator

atztogo commented Jun 17, 2023

Now I can not follow the current status. To merge this PR, what is left to do and what will be postponed?

@lan496 lan496 force-pushed the layer-group-refactor branch from 1de4f12 to 724e786 Compare June 17, 2023 11:23
@lan496 lan496 marked this pull request as ready for review June 17, 2023 11:31
@lan496
Copy link
Member Author

lan496 commented Jun 17, 2023

After the all tests pass and @LecrisUT approves, this PR will be ready to be merged.

@lan496 lan496 merged commit d6fcbe8 into spglib:develop Jun 17, 2023
@lan496 lan496 deleted the layer-group-refactor branch June 17, 2023 11:38
@atztogo
Copy link
Collaborator

atztogo commented Jun 17, 2023

Thanks very much @lan496 and @LecrisUT!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Significant ehancements layer-group
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants